Answers for "how to decorators work in 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

# This function take input from user and return
# list of 0 and 1, zero represents odd number
# and 1 represents even number
def deco(func):
    def wrap(lst):
        x = [1 if i % 2 == 0 else 0 for i in lst]
        return x
        func(lst)
    return wrap

@deco   
def display(l):
    return (l)

a = [int(x) for x in input("Enter number of elements : ").split(",")]
print(display(a))
Posted by: Guest on June-27-2021

Python Answers by Framework

Browse Popular Code Answers by Language