2

I am writing my first python script and this package/module thing has me going nuts. Lets start with my folder layout

builder.py
-buildModule\__init__.py
-buildModule\AppList.py
-buildModule\BuildClass.py
-buildModule\ConfigSystem.py
-buildModule\MenuSystem.py
-buildModule\Singleton.py

ok, so my __init__.py looks like this

from ConfigSystem import *
from BuildClass import *
from MenuSystem import *
from AppList import *
from buildModule.Singleton import Singleton

Now, im trying to decorate my configsystem as a singleton so my singleton looks like

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.')

and now, with my configsystem class, if im reading the manual correctly, this should work

import Singleton


@Singleton
class ConfigSystem:

but im getting

TypeError: 'module' object is not callable

I've read the module part of the manual several times now and im not quite getting it. Why isn't this working?

Is there a module/package tutorial out there somewhere that is written a bit clearer/differently than the manual?

dr jimbob
  • 17,259
  • 7
  • 59
  • 81
scphantm
  • 4,293
  • 8
  • 43
  • 77
  • Why is `ConfigSystem` the name of a module AND the name of a class? Isn't that going to be confusing? Could you change one of those names? It would almost eliminate your `'module' object is not callable` error. – S.Lott Sep 19 '11 at 20:17
  • try "from Singleton import Singleton". Your imported Singleton is not a class but a module. That is what your error message says. – rocksportrocker Sep 19 '11 at 20:18
  • @scphantm Hey, that's _my_ singleton implementation. [Character for character](http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern/7346105#7346105). :D I'm glad you found it useful but beware: it doesn't work if you later want to inherit from that singleton. – Paul Manta Sep 19 '11 at 20:27
  • yea paul, i found it here. still trying to get used to this multiple classes in one file thing. after working in java exclusively for 9 years, its quite a change going to something like python. – scphantm Sep 20 '11 at 13:23

2 Answers2

3

You need to change import Singleton to from Singleton import Singleton (or as Alex suggested change @Singleton to @Singleton.Singleton.

In general you should use qualified imports to avoid namespace collisions, e.g., import SomeModule with a call like SomeModule.SomeClass or for brevity something like import SomeModule as SM with a call like SM.SomeClass or SM.some_function rather than importing everything from a module like from SomeModule import *.

You have name collisions, where Singleton is referring to the module (e.g., Singleton.py a file that is a collection of classes/functions/variables) rather than the Singleton class (class Singleton(object)). Specifically, in your decorator @Singleton Singleton is referring to the module (you import some_module or from some_module import a_class_or_object) rather than a class or function.

dr jimbob
  • 17,259
  • 7
  • 59
  • 81
2

Either change import Singleton to from Singleton import Singleton, or change @Singleton to @Singleton.Singleton.

Alex Smith
  • 1,495
  • 12
  • 13