class A(SomeBaseClass):
def __init__(self):
super().__init__()
self._attribute_0 = something_0
self._attribute_1 = something_1
# etc.
def some_method(self) -> None:
do_something()
Now imagine that you have similar classes B, C, D, etc.
(not all of them inherit from the same base class). All of them have some functionality that is individual to them, and from a logical point of view well encapsulated in these classes. The key aspect is, that for every of these classes, always only one instance will exist at a time. The problem is, sometimes one class needs access to the other's functions, i.e.
if __name__ == "__main__":
a = A()
b = B(a)
c = C()
d = D(c, a)
b.d = d
etc.
This code is reasonable to me assuming there exist multiple instances of class A, B, etc.
, but since always only one instance exists at a time, it seems a bit redundant to always pass them around in the constructor (in reality, the classes have of course more complicated names and the "main" function becomes very hard to read...).
One solution, although controversial, in other languages would be the Singelton pattern. And there seem to be ways to implement singeltons in python (see here), but they seem to be discouraged (i.e. some people go as far to say as that they are never needed in python).
My goal is fairly simple: I would like my objects a,b,c, etc.
to be accessible in the entire project without having to pass them around in a constructor or set them one-by-one via b.d = d
, etc.
Is this possible in Python? What are my options?
Illustration of answer by @blue_note
# objects.py
a = A()
b = B(a)
c = C()
d = D(c, a)
b.d = d
and then when you need them somewhere else:
# foo.py
from objects.py import a
class Foo:
def __init__(self):
# use a