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.
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.
return z + 1
return z - 1
def main(func, z):
result = func(z)
also, a function can return another function
wer = call_fun()
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
On run this will provide o/p like this:
# on decorating initial function
>> decorate = deco(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.
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):
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 one(m, n):
print("divide", a, "and", b)
if n == 0:
print("oops! Can’t be divisible")
return func(m, n)
def division(m, n):
divide 2 and 5
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 inner(*args, **kwargs):
print("decorate any function")
return func(*args, **kwargs)
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.