Can't I define a decorator in a separate file and import it?
Ben Finney
ben+python at benfinney.id.au
Thu Dec 22 17:33:22 EST 2011
Saqib Ali <saqib.ali.75 at 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
More information about the Python-list
mailing list