Can't I define a decorator in a separate file and import it?

Saqib Ali saqib.ali.75 at gmail.com
Thu Dec 22 16:29:04 EST 2011


Thanks for pointing out the mistake!
Works.
- Saqib




On Thu, Dec 22, 2011 at 4:31 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> 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~
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111222/df450aff/attachment-0001.html>


More information about the Python-list mailing list