Recently, I was reading some articles on Python decorator. It motives me since a phone interview. Before that, I wasn’t paying much attention to it. But later one, I found it a very good tool to do the dynamic programming.
Below are some online sources that I collect, I try to make it more or like the Q-A style:
#Some good links:
– Decorator home: pypi.python.org/pypi/decorator
– Computing Thoughts: http://www.artima.com/weblogs/viewpost.jsp?thread=240808
#One example code for decorator used for logging information:
#Neat things to do with Python decorators – Ubuntu Forums
Q: How to show the help information defined in original function, after the function is decorated.
A: use functools.wraps() The Standard Library doc for functools (http://docs.python.org/library/functools.html)
Q: An example worthy understanding more
Notes: The function like wrap the inner original function, with additional def-functions, it can most utilize the original function or class, but most likely work when initialization.
Another thing is that we need to discriminate the “decorator” and “sub-or-super-class structure”.
Q: oop – What is a metaclass in Python? – Stack Overflow
Links-> ; http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python
Q: Understanding Python decorators – Fantastic Answer
Q: A well introduction of decorator
Q: Is there some build-in functions can be used as decorator?
A: functools module contains some fucntions that can be used as decorators, but they aren’t built-ins.
As a side notes: we can find the list of Built-in functions at: Python glossary
Also PythonDecoratorLibrary may give some good examples for using of decorator
Q: Discussion on “a-ha this looks liek a job for decorator”
A: Good examples:Bruce Eckel on Decorators and Example : A Decorator-Based Build System
An example that I wrote using decorator for dynamic programming.