Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all decorated functions in a module

Tags:

python

Is it possible to find out if a function is decorated at runtime? For example could I find all functions in a module that are decorated by "example"?

@example
def test1():
    print "test1"
like image 1000
TheDude Avatar asked Jan 27 '10 02:01

TheDude


People also ask

What is a decorated function Python?

A decorator in Python is a function that takes another function as its argument, and returns yet another function . Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.

How do you find the function of a module in Python?

We can list down all the functions present in a Python module by simply using the dir() method in the Python shell or in the command prompt shell.

Which library function returns the list of all functions in a module?

The dir() function The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the modules, variables, and functions that are defined in a module.

What command can we use to obtain a list of all functions inside the module?

You can use dir(module) to see all available methods/attributes.


1 Answers

Since you have indicated that you have the control over the wrapper code, here is an example:

def example(f):
    f.wrapped = True
    return f

@example
def test1():
    print "test1"

def test2():
    print "test2"


print test1.wrapped
print hasattr(test2, 'wrapped')
like image 68
John La Rooy Avatar answered Oct 10 '22 23:10

John La Rooy