Can't I define a decorator in a separate file and import it?
Ethan Furman
ethan at stoneleaf.us
Thu Dec 22 16:31:53 EST 2011
Saqib Ali wrote:
> MYCLASS.PY:
>
> #!/usr/bin/env python
> import os, sys, string, time, re, subprocess
> import Singleton
This should be 'from Singleton import Singleton'
> @Singleton
> class myClass:
>
> def __init__(self):
> print 'Constructing myClass'
At this point, the *instance* of myClass has already been constructed
and it is now being initialized. The __new__ method is where the
instance is actually created.
>
> def __del__(self):
> print 'Destructing myClass'
>
>
> class Singleton:
>
> def __init__(self, decorated):
> self._decorated = decorated
>
> def Instance(self):
> try:
> return self._instance
> except AttributeError:
> self._instance = self._decorated()
> return self._instance
>
> def __call__(self):
> raise TypeError(
> 'Singletons must be accessed through the `Instance`
> method.')
~Ethan~
More information about the Python-list
mailing list