[Python-Dev] Re: metaclass and author are already decorators (original) (raw)
Paul Morrow pm_mon at yahoo.com
Sun Aug 22 00:54:28 CEST 2004
- Previous message: [Python-Dev] Re: __metaclass__ and __author__ are already decorators
- Next message: [Python-Dev] Re: __metaclass__ and __author__ are already decorators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Bob Ippolito wrote:
On Aug 21, 2004, at 6:24 PM, Paul Morrow wrote:
It seems that writing a decorator is going to be a bizarre experience. In the example, I would need to write a function named 'decoration' that returns a function that will recieve a function (foo) to be decorated and then return a function. Does that sound about right? Yes that is correct. What would functions like 'decoration' typically look like? Could you show a short code snippet? http://python.org/peps/pep-0318.html
Thanks. Of the 5 examples there, the first two are apparently not implemented correctly, as they expect that the function/class to be decorated is passed directly to them, rather than to the function they return. Would you agree? I pasted them here for your consideration...
1. Define a function to be executed at exit. Note that the function
isn't actually "wrapped" in the usual sense.
def onexit(f): import atexit atexit.register(f) return f
@onexit def func(): ...
2. Define a class with a singleton instance. Note that once the
class disappears enterprising programmers would have to be more creative to create more instances. (From Shane Hathaway on python-dev.)
def singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance
@singleton class MyClass: ...
- Previous message: [Python-Dev] Re: __metaclass__ and __author__ are already decorators
- Next message: [Python-Dev] Re: __metaclass__ and __author__ are already decorators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]