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.8k7 gold badges63 silver badges84 bronze badges
asked Sep 19, 2011 at 20:13
4
  • 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. Commented Sep 19, 2011 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. Commented Sep 19, 2011 at 20:18
  • @scphantm Hey, that's my singleton implementation. Character for character. :D I'm glad you found it useful but beware: it doesn't work if you later want to inherit from that singleton. Commented Sep 19, 2011 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. Commented Sep 20, 2011 at 13:23

2 Answers 2

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.

answered Sep 19, 2011 at 20:16
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Sep 19, 2011 at 20:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.