Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17756

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

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Can't I define a decorator in a separate file and import it?
References <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <7fb2b7f5-12a2-4724-88e2-53991004d079@d8g2000vbb.googlegroups.com>
Date 2011-12-23 09:33 +1100
Message-ID <87d3bgkzil.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web