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

Chris Kaynor ckaynor at zindagigames.com
Thu Dec 22 16:15:32 EST 2011


On Thu, Dec 22, 2011 at 1:11 PM, Saqib Ali <saqib.ali.75 at gmail.com> wrote:

> MYCLASS.PY:
>
> #!/usr/bin/env python
> import os, sys, string, time, re, subprocess
> import Singleton
>

This imports the module Singleton, not the class or function.

There are two options to deal with this:

from Singleton import Singleton

imports the name Singleton from the module Singleton, which will be what
you are probably expecting. If you wish to keep the namespace, you can also
change your call from @Singleton to @Singleton.Singleton


>
> @Singleton
> class myClass:
>
>     def __init__(self):
>         print 'Constructing myClass'
>
>     def __del__(self):
>         print 'Destructing myClass'
>
>
> SINGLETON.PY:
>
>
> #!/usr/bin/env python
> import os, sys, string, time, re, subprocess
> import Singleton
>
>
> @Singleton
> class myClass:
>
>     def __init__(self):
>         print 'Constructing myClass'
>
>     def __del__(self):
>         print 'Destructing myClass'
>
> TRACEBACK:
>
> >>> import myClass
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "myClass.py", line 6, in <module>
>     @Singleton
> TypeError: 'module' object is not callable
>
>
>
> - Saqib
>
>
>
>
>
>>>
>> Post the code, and the traceback.
>>
>> ~Ethan~
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111222/c09ad13a/attachment-0001.html>


More information about the Python-list mailing list