Finding decorators in a file
Ryan Ginstrom
software at ginstrom.com
Fri Oct 26 23:32:03 EDT 2007
> On Behalf Of Andrew West
> Basically what I'm looking for is a way to, given a python file, look
> through that file and find all the decorators and the associated
> functions, that includes any arguments that the decorator has.
The inspect module has a function called "findsource"
import inspect
import my_module
lines, line_num = inspect.findsource(my_module)
decorated_lines = [num
for num, line in enumerate(lines)
if line.strip().startswith("@")]
Probably a little more complicated than that -- like what if a function has
two decorators? -- but I think the basic idea will work.
Regards,
Ryan Ginstrom
More information about the Python-list
mailing list