Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name of a decorated function? [duplicate]

here is my decorator:

def check_domain(func):

    def wrapper(domain_id, *args, **kwargs):
        domain = get_object_or_None(Domain, id=domain_id)
        if not domain:
            return None
        return func(domain_id, *args, **kwargs)

    return wrapper

Here is a wrapped up function:

@check_domain
def collect_data(domain_id, from_date, to_date):
    do_stuff(...)

If I do collect_data.__name__ I get wrapper instead of collect_data

Any ideas?

like image 835
RadiantHex Avatar asked Feb 03 '11 14:02

RadiantHex


People also ask

What is decorated function in 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 I find the name of my class decorator?

Use the type() function and __name__ to get the type or class of the Object/Instance. Using the decorator to get the type or class of the Object/Instance.

What is function decoration?

Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.


4 Answers

functools.wraps is not needed! Just use func.__name__

import time

def timeit(func):
    def timed(*args, **kwargs):
        ts = time.time()
        result = func(*args, **kwargs)
        te = time.time()
        print('Function', func.__name__, 'time:', round((te -ts)*1000,1), 'ms')
        print()
        return result
    return timed

@timeit
def math_harder():
    [x**(x%17)^x%17 for x in range(1,5555)]
math_harder()

@timeit
def sleeper_agent():
    time.sleep(1)
sleeper_agent()

Outputs:

Function math_harder time: 8.4 ms
Function sleeper_agent time: 1003.7 ms
like image 157
Zach Estela Avatar answered Nov 11 '22 11:11

Zach Estela


You may want to use wraps from functools. See the example

>>> from functools import wraps
>>> def my_decorator(f):
...     @wraps(f)
...     def wrapper(*args, **kwargs):
...         print('Calling decorated function')
...         return f(*args, **kwargs)
...     return wrapper
...
>>> @my_decorator
... def example():
...     """Docstring"""
...     print('Called example function')
...
>>> example()
Calling decorated function
Called example function
>>> example.__name__
'example'
>>> example.__doc__
'Docstring'
like image 45
Tommaso Barbugli Avatar answered Nov 11 '22 12:11

Tommaso Barbugli


In addition to functools.wraps, you can check out the decorator module which was designed to help with this problem.

like image 6
tkerwin Avatar answered Nov 11 '22 11:11

tkerwin


Use http://docs.python.org/library/functools.html#functools.wraps

like image 3
Tomasz Zieliński Avatar answered Nov 11 '22 11:11

Tomasz Zieliński