Is storing class instances in a class variable a good pattern

One design pattern that I use a lot in Python is this:

class Foo:
    foos = []
    def __init__(self, argument):
        initialize_instance()
        foos.append(self)

      

Does this have a name and is it considered useful?

+3


source to share


1 answer


I really don't think this is good practice in OOP (I'm not an expert in Python). Using static (like a list foos

) is almost always a bad concept. As said in the comments, the best solution is to set up a service class (view SOA pattern in OOP) to index all your instances Foo

and then retrieve them.



However, do not assume that this is a variant of the registry template. It is rather an antipattern to this pattern, preventing proper low cohesion between classes. Granted, Python is not the best language to do strong OOP and respect that, but I would not recommend doing it anyway.

+1


source







All Articles