I find myself repeating a common pattern when trying to execute code across multiple objects.
Arg_list_one = ["first","second", "so on"]
Arg_list_two = ["first","second", "so on"]
MANIFEST = [ ]
class connection(object):
def __init__(self, args):
...
MANIFEST.append(self)
def Run(self):
...
connection(Arg_list_one)
connection(Arg_list_two)
[conn.Run() for conn in MANIFEST]
Is this a pattern (or anti-pattern)? Or just something that I made up?
Are there other, better, ways of doing this?
Why would you need a list of all objects ever created? Many of those may belong to completely unrelated pieces of your application! A given piece of code shouldn’t assume it’s the only one to user a class. Especially since there’s usually no need to:
A more practical and less stylistic issue is that this list would keep every single object of that class that’s ever instanciated alive forever. And they say one can’t create memory leaks in Python… (This can be avoided with weak reference, but would make the code much more complex to transparently remove dead references.)
The solution is barely more typing and saves you a whole lot of headaches later on. Next you’ll use local variables to save yourself a
return?That said, there may be situations where such a list may be useful (with a fix for said memory leak, of course). I just don’t see anything close to that in your example, and I think such situations are very rare.