Answers for "decorator library python"

19

decorator python

def our_decorator(func):
    def function_wrapper(x):
        print("Before calling " + func.__name__)
        func(x)
        print("After calling " + func.__name__)
    return function_wrapper

@our_decorator
def foo(x):
    print("Hi, foo has been called with " + str(x))

foo("Hi")
Posted by: Guest on September-30-2020
0

decorators in python

def deco(function):
    def wrap(num):
        if num % 2 == 0:
            print(num,"is even ")
        else:
            print(num,"is odd")
        function(num)
    return wrap
    
@deco
def display(num):
    return num
display(9)   # pass any number to check whether number is even or odd
Posted by: Guest on June-25-2021
0

decorators in python

def deco(func):
    def wrap(s):
        d = {}
        for i in s:
            d[i] = s.count(i)
        func(s)
        return d
    return wrap

@deco
def count(stng):
    return stng
mystring = "scorpio"  
print(count(mystring))
Posted by: Guest on June-28-2021

Python Answers by Framework

Browse Popular Code Answers by Language