Re: How to get the source code of python function being decorated?
Peng Yu wrote: > Hi, See the following example, I am not able to get the source code of > the actual function that does the calculation of partial_ratio. Does > anybody know what is the correct way of getting the source? > > /tmp$ ./main.py > @functools.wraps(func) > def decorator(*args, **kwargs): > if args[0] is None or args[1] is None: > return 0 > return func(*args, **kwargs) > > /tmp$ cat ./main.py > #!/usr/bin/env python > # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 > # fileencoding=utf-8: > > import fuzzywuzzy.fuzz > import inspect > print inspect.getsource(fuzzywuzzy.fuzz.partial_ratio) In Python 3 functools.wraps() records the wrapped function as __wrapped__: $ cat tmp.py import functools def spam(func): @functools.wraps(func) def decorator(*args, **kwargs): return func(*args, **kwargs) return decorator @spam def ham(foo, bar): return 42 $ python3 ... >>> import inspect >>> import tmp >>> print(inspect.getsource(tmp.ham)) @functools.wraps(func) def decorator(*args, **kwargs): return func(*args, **kwargs) >>> print(inspect.getsource(tmp.ham.__wrapped__)) @spam def ham(foo, bar): return 42 In Python 2 when you look into the attributes of tmp.ham() using dir() you'll eventually find >>> print inspect.getsource(tmp.ham.__closure__[0].cell_contents) @spam def ham(foo, bar): return 42 but I'm not sure this is general enough to spare you the look into the source code. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to get the source code of python function being decorated?
On Friday, September 16, 2016 at 3:20:15 PM UTC-4, Peng Yu wrote: > Hi, See the following example, I am not able to get the source code of > the actual function that does the calculation of partial_ratio. Does > anybody know what is the correct way of getting the source? > > /tmp$ ./main.py > @functools.wraps(func) > def decorator(*args, **kwargs): > if args[0] is None or args[1] is None: > return 0 > return func(*args, **kwargs) > > /tmp$ cat ./main.py > #!/usr/bin/env python > # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 > fileencoding=utf-8: > > import fuzzywuzzy.fuzz > import inspect > print inspect.getsource(fuzzywuzzy.fuzz.partial_ratio) > In general, you can't get at the decorated function. Decorators can do anything they want with the function they decorate, including completely ignore it, or store it in a closure, assign it to a global, add it to a data structure, etc. Can you tell us more about the problem you are trying to solve? Why do you want a program that access the source of decorated functions? Maybe there's another way to get at what you need. --Ned. -- https://mail.python.org/mailman/listinfo/python-list
How to get the source code of python function being decorated?
Hi, See the following example, I am not able to get the source code of the actual function that does the calculation of partial_ratio. Does anybody know what is the correct way of getting the source? /tmp$ ./main.py @functools.wraps(func) def decorator(*args, **kwargs): if args[0] is None or args[1] is None: return 0 return func(*args, **kwargs) /tmp$ cat ./main.py #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8: import fuzzywuzzy.fuzz import inspect print inspect.getsource(fuzzywuzzy.fuzz.partial_ratio) -- Regards, Peng -- https://mail.python.org/mailman/listinfo/python-list