Python Decorators Code Snippets

Posted by

Hello Coders,

We have discussed python decorators on the Youtube channel. If you go through this, you will have a clear understanding of the decorators and also it will help to crack the interviews.

Here are the code snippets of the Videos.

  1. Python decorators basic Link Here
# decorator definition
def decorator_function(base_fx):
    def wrapper_function():
        print("extra code are executed before the base function call")
        print("some more line are added...")
       return base_fx()
    return wrapper_function


# decorator call and base function definition
@decorator_function
def base_function():
    print("base function executed")

# base function call
base_function()
def base_function():
    print("base function executed 123 ")

display = decorator_function(base_function)
display()

2. Python decorators base functions with arguments Link Here

# decorator definition

def decorator_function(base_fx):
    def wrapper_function(x,y):
        print("extra codes are executed before base function... ")
        base_result = base_fx(x,y)
    return wrapper_function


@decorator_function
def base_function(arg1, arg2):
    print(f"arg1 = {arg1} and arg2 = {arg2}")

# base funtion call
base_function(5,6)

3.  Python decorators with arguments Link Here

# decorator definition
def deco_1(text):
    def decorator_function(base_fx):
        def wrapper_function(x,y):
            print(text)
            return base_fx(x,y)
        return wrapper_function
    return decorator_function


# decorator call and base function definition


@deco_1("india")
@deco_1("korea")
@deco_1("usa")
def display(x,y):
    print(f"value of x = {x} and value of y = {y}")

# base call
display(5,6)

4. Python decorators with chaining Link Here

# deco_1 definition

def deco_1(fx):
    def wrapper(arg1,arg2):
        print("from deco_1 before base function call....")
        fx(arg1,arg2)
        print("from deco_1 after base function call...")
    return wrapper

# deco_2 definition

def deco_2(fxx):
    def wrapper(arg3,arg4):
        print("from deco_2 before base function call")
        fxx(arg3,arg4)
        print("from dec_2 after base function call")
    return wrapper

@deco_1
@deco_2
def base_function(x,y):
    print(f"value of x = {x} and value of y = {y}")

# base function call
base_function(5,6)

5. Python decorators chaining with arguments Link Here

# deco_1 definition
def deco_1(text):
    def base_fx_recieve(base_fx):
        def base_fx_args(arg1,arg2):
            print(text)
            return base_fx(arg1,arg2)
        return base_fx_args
    return base_fx_recieve


# deco_2 definition

def deco_2(text):
    def base_fx_recieve(base_fx):
        def base_fx_args(arg1,arg2):
            print(text)
            return base_fx(arg1,arg2)
        return base_fx_args
    return base_fx_recieve


@deco_2("India")
@deco_1("Computer Science")
def base_function(x,y):
    print(f"value of x = {x} and value of y = {y}")

base_function(5,6)

5. Python property decorators Link Here

class Cricket:

    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    @property
    def fullname(self):
        return self.first_name + ' ' + self.last_name

player_1 = Cricket("Jhonty","Rhodes")
player_1.fullname

Leave a Reply

Your email address will not be published.