Is it bad practice to initialize the objects in the module, in the module code?
in Module.py
:
class _Foo(object):
def __init__(self):
self.x = 'Foo'
Foo = _Foo()
Than in user code, you could:
>>> from Module import Foo
>>> print Foo.x
'Foo'
>>>
...without having to initialize the Foo class in the user code. Of course, only useful if you don't need arguments to initialize the object.
Is there a reason not to do this?
@staticmethod
and class variables if you only need one instance – Flustaticmethod
and class variables, you can useclassmethod
and class variables. That's a poor way to create a singleton though. – Townspeoplestaticmethod
s instead of using the automatically passed reference to the class fromclassmethod
s. – Townspeople