I use decorators mainly for timing purposes
def time_dec(func):
def wrapper(*arg):
t = time.clock()
res = func(*arg)
print func.func_name, time.clock()-t
return res
return wrapper
@time_dec
def myFunction(n):
...
More Related Contents:
- How to make function decorators and chain them together?
- Creating a singleton in Python
- Decorators with parameters?
- How to get method parameter names?
- Preserving signatures of decorated functions
- Calling class staticmethod within the class body?
- How to decorate a class?
- How can I separate the functions of a class into multiple files?
- Attaching a decorator to all functions within a class
- Decorator execution order
- How to create a decorator that can be used either with or without parameters?
- How to inject variable into scope with a decorator?
- Introspection to get decorator names on a method?
- How to decorate a method inside a class?
- Python Class Based Decorator with parameters that can decorate a method or a function
- What is the best way to do automatic attribute assignment in Python, and is it a good idea?
- How do I make function decorators and chain them together?
- How to make function decorators and chain them together in Python?
- Python decorator? – can someone please explain this? [duplicate]
- How to do a conditional decorator in python?
- How can I pass a variable in a decorator to function’s argument in a decorated function?
- Python decorator as a staticmethod
- How to create a custom decorator in Django?
- Currying decorator in python
- Python functools.wraps equivalent for classes
- Python decorator best practice, using a class vs a function
- How to auto register a class when it’s defined
- Auto-register class methods using decorator
- Decorator error: NoneType object is not callable
- How to skip a pytest using an external fixture?