Python Decorator - The best example ever
The best example to show how decorators work.
Yes, there are a lot of resources to explain how decorators works. A lot of them are confusing. Others, are explaining the history that it was named a pie before it is called decorator and it was rejected then introduced in the conference and blah blah....
Luckily I found here concise example that shows how decorators work. Just copy and paste it in Python Interpreter and see how it works.
def shout(wrapped):
def inner(*args, **kwargs):
print('BEFORE!')
ret = wrapped(*args, **kwargs)
print('AFTER!')
return ret
return inner
@shout
def doge():
print('such wow!')
resources:
def inner(*args, **kwargs):
print('BEFORE!')
ret = wrapped(*args, **kwargs)
print('AFTER!')
return ret
return inner
@shout
def doge():
print('such wow!')
resources:
https://www.codeschool.com/blog/2016/05/12/a-guide-to-python-decorators/
https://wiki.python.org/moin/PythonDecorators
https://www.python.org/dev/peps/pep-0318/
https://wiki.python.org/moin/PythonDecorators
https://www.python.org/dev/peps/pep-0318/
Comments
Post a Comment