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: ...
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4