Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17749 > unrolled thread
| Started by | Saqib Ali <saqib.ali.75@gmail.com> |
|---|---|
| First post | 2011-12-22 12:53 -0800 |
| Last post | 2011-12-22 13:07 -0800 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
Can't I define a decorator in a separate file and import it? Saqib Ali <saqib.ali.75@gmail.com> - 2011-12-22 12:53 -0800
Re: Can't I define a decorator in a separate file and import it? Saqib Ali <saqib.ali.75@gmail.com> - 2011-12-22 13:09 -0800
Re: Can't I define a decorator in a separate file and import it? Ben Finney <ben+python@benfinney.id.au> - 2011-12-23 09:33 +1100
Re: Can't I define a decorator in a separate file and import it? Andrew Berg <bahamutzero8825@gmail.com> - 2011-12-22 15:20 -0600
Re: Can't I define a decorator in a separate file and import it? Ethan Furman <ethan@stoneleaf.us> - 2011-12-22 13:31 -0800
Re: Can't I define a decorator in a separate file and import it? Ethan Furman <ethan@stoneleaf.us> - 2011-12-22 13:07 -0800
| From | Saqib Ali <saqib.ali.75@gmail.com> |
|---|---|
| Date | 2011-12-22 12:53 -0800 |
| Subject | Can't I define a decorator in a separate file and import it? |
| Message-ID | <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> |
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 work. Why can't I import this Singleton decorator from a different file? What's the best work around?
[toc] | [next] | [standalone]
| From | Saqib Ali <saqib.ali.75@gmail.com> |
|---|---|
| Date | 2011-12-22 13:09 -0800 |
| Message-ID | <7fb2b7f5-12a2-4724-88e2-53991004d079@d8g2000vbb.googlegroups.com> |
| In reply to | #17749 |
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):
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.')
Here is 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'
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-12-23 09:33 +1100 |
| Message-ID | <87d3bgkzil.fsf@benfinney.id.au> |
| In reply to | #17750 |
Saqib Ali <saqib.ali.75@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 module bound to the name ‘foo’. A
module is not callable.
> Here is Singleton.py:
You should name the file ‘singleton.py’ instead; modules should be named
in all lower-case, as PEP 8 recommends.
> Here is myClass.py:
Which should be named ‘my_class.py’, instead, by the same rationale.
Neither of those is necessary, but it will help dispel the confusion
you're experiencing, and help your code be more easily comprehensible to
other Python programmers.
> #!/usr/bin/env python
> import os, sys, string, time, re, subprocess
> import Singleton
So, you should (after the renames suggested) do either::
import singleton
@singleton.Singleton
class my_class:
or::
from singleton import Singleton
@Singleton
class my_class:
Notice how naming the module in lower-case makes it more easily
distinguishable from the class name.
More generally, remember that Python is not Java
<URL:http://dirtsimple.org/2004/12/python-is-not-java.html>, and you
should be grouping your classes into logically coherent files, not
one-file-per-class.
--
\ “Reality must take precedence over public relations, for nature |
`\ cannot be fooled.” —Richard P. Feynman, _Rogers' Commission |
_o__) Report into the Challenger Crash_, 1986-06 |
Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Andrew Berg <bahamutzero8825@gmail.com> |
|---|---|
| Date | 2011-12-22 15:20 -0600 |
| Message-ID | <mailman.3996.1324588811.27778.python-list@python.org> |
| In reply to | #17749 |
You have the same code for both files...
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-12-22 13:31 -0800 |
| Message-ID | <mailman.3999.1324589276.27778.python-list@python.org> |
| In reply to | #17749 |
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~
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-12-22 13:07 -0800 |
| Message-ID | <mailman.4001.1324589676.27778.python-list@python.org> |
| In reply to | #17749 |
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 file, it doesn't work. > > Why can't I import this Singleton decorator from a different file? > What's the best work around? Post the code, and the traceback. ~Ethan~
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web