I am learning about classes in python right now. My apologies if my language is a little sloppy, but I'm still trying to understand exactly how classes work.
While working through some projects of my own, I often find myself wanting to add an object, upon initialization, to a list so I can keep a track of all the objects I have made of that type. I have been trying to do this in the following way:
class My_Class(object):
Object_List=[]
def __init__(self):
Object_List.append(self)
However, from what I can tell, the "_ _init _ _" suite doesn't have object list in it's namespace. If this is correct, it would seem to make sense, because functions have their own namespace. However the issue then is the above code doesn't work. I don't seem to be able to add my objects to a globally defined list.
Can anyone suggest another way I could accomplish the same thing, and end up with a list of all the My_Class objects I have created?