Decorators and how they relate to Python - A little insight please!

Fuzzyman fuzzyman at gmail.com
Sat Oct 21 17:57:48 EDT 2006


Fredrik Lundh wrote:
> Jerry wrote:
>
> > even though I've read the PEP
>
> even the examples section?
>
>      http://www.python.org/dev/peps/pep-0318/#examples
>

The second example of which shows :

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:
    ...


:-o

Fuzzyman
http://www.voidspace.org.uk/


> if you want more examples, see the cookbook
>
> http://www.google.com/search?q=+site%3Aaspn.activestate.com+decorator+cookbook
> 
> </F>




More information about the Python-list mailing list