Last active
December 24, 2015 08:29
-
-
Save pratikmallya/6771130 to your computer and use it in GitHub Desktop.
This piece of code is what I'm using to clarify certain function-related concepts in Python, especially those relating to decorators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Test decorator functionality in Python | |
def log(F): | |
def print_args(*args, **kwargs): | |
print args, kwargs | |
return print_args | |
@log | |
def holy_smokes(a, b, c): | |
print a + b + c | |
if __name__ == "__main__": | |
holy_smokes(1,2,3) |
Nice Resources on Decorators:
http://www.siafoo.net/article/68
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Question: how are the args of
holy_smokes
being printed, when there is no clear relation between that andprint_args
? I cannot see how this works, using my standard view of functions: clearly,log
is given an input argumentholy_smokes
, so we should substituteholy_smokes
instead ofF
in the function definition. And what isreturned? A different function! This new function does only one thing: prints out values of its input arguments. It has no relation to the input function
F
!So there's nothing mysterious going on here. You're just making the same mistake repeatedly of not being able to distinguish between function definition and function call!