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

2011-12-22 Thread Saqib Ali
I'm using this decorator to implement singleton class in python: http://stackoverflow.com/posts/7346105/revisions The strategy described above works if and only if the Singleton is declared and defined in the same file. If it is defined in a different file and I import that file, it doesn't

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

2011-12-22 Thread Saqib Ali
MYCLASS.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' SINGLETON.PY: #!/usr/bin/env python import os,

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

2011-12-22 Thread Saqib Ali
BTW Here is the 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 Here is Singleton.py: class Singleton: def __init__(self, decorated):

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

2011-12-22 Thread Chris Kaynor
On Thu, Dec 22, 2011 at 1:11 PM, Saqib Ali saqib.ali...@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

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

2011-12-22 Thread Andrew Berg
You have the same code for both files... -- http://mail.python.org/mailman/listinfo/python-list

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

2011-12-22 Thread Ethan Furman
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

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

2011-12-22 Thread Saqib Ali
Thanks for pointing out the mistake! Works. - Saqib On Thu, Dec 22, 2011 at 4:31 PM, Ethan Furman et...@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

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

2011-12-22 Thread Ethan Furman
Saqib Ali wrote: I'm using this decorator to implement singleton class in python: http://stackoverflow.com/posts/7346105/revisions The strategy described above works if and only if the Singleton is declared and defined in the same file. If it is defined in a different file and I import that

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

2011-12-22 Thread Marc Christiansen
Saqib Ali saqib.ali...@gmail.com wrote: I'm using this decorator to implement singleton class in python: http://stackoverflow.com/posts/7346105/revisions The strategy described above works if and only if the Singleton is declared and defined in the same file. If it is defined in a

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

2011-12-22 Thread Ben Finney
Saqib Ali saqib.ali...@gmail.com writes: BTW Here is the 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 Yes. When you ‘import foo’, you have a