Author - StudySection Post Views - 112 views
Python

Decorators with Python

There is a very interesting feature in python known as Decorators, that is used to add additional functionality to the existing code.

Because this feature tries to modify the existing code functionality, this is known as metaprogramming.

Things that one should know before starting with decorators

One must be completely familiar with the concept of python, that everything in python is an object. These objects can be recognized using names that we give to objects. Functions are also objects with attributes. One can give many diff names to the same function object.
e.g.
def one(a):
print(a)
one(‘asd’)
two=one
two(‘asd’)0
o/p:
asd
asd

So in the above example, both functions one and two give the same result because both names refer to the same function object.

After that things are getting a bit more complicated. Because a function can be passed to another function as an argument.
For example, we use reduce and filter functions in python.
Functions like these can take other functions as arguments are known as higher-order functions.
e.g.
make functions
def increment(z):
return z + 1
def decrement(z):
return z - 1
def main(func, z):
result = func(z)
return result

call functions
main(increment,1)
main(decrement,1)
o/p
2
0

also, a function can return another function
def call_fun():
def return_fun():
print("yo")
return return_fun
wer = call_fun()
wer()
o/p
Yo

In the above example, return_fun returns the call_fun when called.

The objects which allow the __call__() method are called callable. So, a decorator is a callable object that returns a callable. In general, one can say, a decorator takes a function and adds some functionality, and returns it.

Now let’s consider an e.g that shows us how to decorate a function
def deco(func):
def inner():
print("decorated")
func()
return inner
def initial():
print("initial")
On run this will provide o/p like this:
>> initial()
initial
# on decorating initial function
>> decorate = deco(initial)
>> decorate()
decorated
initial

Here, in the above example deco() is a decorator. The function initial() is decorated by function deco(). Here as one can see how deco() changes or we can say add some new functionality to the initial function. It’s similar to wrapping a thing, so one can refer to a decorator as a wrapper within which some new or changed functionalities can be wrapped.

Note: the function carried out by the decorator will never gonna alter.

That’s why one can use @ symbol followed by the decorator function name and write it just above the function that one wants to decorate.

Just like
@deco
def initial():
print("initial")

Now let’s discuss how one can decorate a function in other ways like with parameters.
Let’s consider an example
def division(m, n):
return m/n

We have two parameters m,n in the above function. Here if value 0 is passed for n then it’ll throw an error.
So now we’re going to decorate the division().
def deco_divide(func):
def one(m, n):
print("divide", a, "and", b)
if n == 0:
print("oops! Can’t be divisible")
return
return func(m, n)
return inner
@deco_divide
def division(m, n):
print(m/n)
>> division(2,5)
divide 2 and 5
0.4
>> division(2,0)
divide 2 and 0
oops! Can’t be divisible

One can notice that the parameters of the draw inside the inner() function decorator is the same as the parameters of functions it decorates.
We can say that the decorator can work with any no of arguments. So, one can also use *agrs and **kwargs in function to decorate.

Note: *agrs works as a tuple of positional arguments and **kwargs works as a dictionary of positional arguments.
def all_works(func):
def inner(*args, **kwargs):
print("decorate any function")
return func(*args, **kwargs)
return inner

People having good knowledge of Financial accounting can get an accounting certification from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial Accounting or you can go for advanced level certification if you have expert level skills in Financial Accounting.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri