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"
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.
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.
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.
You can use dir(module) to see all available methods/attributes.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With