just looking for opinions or thoughts about how you choose between using a static class vs a memoized class.
For example, consider these 2 python classes:
@cached
class A:
def __init__(self):
#expensive computation
def a(self):
#do something
this class would be called like:
A().a()
now consider this second class:
class B:
is_init = False
@classmethod
def __init(cls):
#expensive computation
cls.is_init=True
@classmethod
def b(cls):
if not cls.is_init:
cls.__init()
#do stuff
called like:
B.b()
they both only do the expensive computation once - so, which one is the better approach? what are the tradeoffs?
not needed for this question:)
Here is the most basic approach I can think of, and it's a perfectly reasonable approach:
Then, you can just do:
Another alternative, which is essentially equivalent:
The key issue isn't really whether you should make it a class variable or an instance variable, IMO, it's that you have a function that initializes your connection that is cached. Now you can utilize that function anywhere you might need a connection. When I said I would just use dependency injection, I mean I would have: