49 decorators in python

 '''

1. used to modify the behaviour of function or class.

Using decorators we can change a behaiviar of function 

without changing any fucntion code.


2. using @ we can assign decorator to the function


## How to create decorator

Decorator fucntion takes one argument as function

Decorator function contains another wrapper function

Inside wrapper function only we have to call function argument

Decorator function will return wrapper function



# Decorator are used to create the static method

class One():

    @staticmethod

    def add():

        print(' \n Static Method')


#ins = One()

#ins.add()

One.add()


#Using @classmethod decorator we can create Class method.

class One():

    name = 'umamahesh'


    @classmethod

    def sub(cls, n):

        print(' \n id of One : ', id(One))

        print(' \n id of cls : ', id(cls))

        cls.name = n

        print(' \n class variable is :: ', One.name)


#ins = One()

#ins.sub('kumar')

One.sub('Nagesh')



# Decorator with abstract ethod

from abc import ABC,abstractmethod


class animal(ABC):

    @abstractmethod

    def eat(self): #abstrct method declaration

        pass


class tiger(animal):

    def eat(self):

        print("tiger eat no-veg")


class cow(animal):

    def eat(self):

        print("cow eat veg")



to=tiger()

to.eat()

co=cow()

co.eat()






'''

def dec_fun(f):

   def wrap_fun():

       return '"{0}"'.format(f())

   return wrap_fun


@dec_fun

def get_name():

   return 'umamahesh'


r = get_name()

print(r)


## decorator with parameters

def dec_fun(f):

   def wrap_fun(name):

       return ' Hellow ' + f(name)

   return wrap_fun


@dec_fun

def get_name(name):

   return name


r = get_name('sriram')

print(r)




Comments

Popular posts from this blog

1 PYTHON PROGRAMMING

4 Tuple data types

2 String data types