__init__ & attributes clarification
Afanasiy
abelikov72 at hotmail.com
Sun Feb 2 17:29:22 EST 2003
More information about the Python-list mailing list
Sun Feb 2 17:29:22 EST 2003
- Previous message (by thread): __init__ & attributes clarification
- Next message (by thread): __init__ & attributes clarification
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, 02 Feb 2003 17:05:30 -0500, Peter Hansen <peter at engcorp.com> wrote: >Afanasiy wrote: >> >> Are these two, in effect, the same? >> >> class hive: >> workers = [] >> >> ... >> >> class hive: >> def __init__(self): >> self.workers = [] > >Definitely not. Understand that when you define the class, >you are not creating an instance of the class, but just the >"template" for instances (which are called objects). > >The __init__ method is used to initialize an *instance* of >a class, which is created when you do "x = hive()". If you >create another instance, "y=hive()", you have two of them, >and each one has its own "workers" list stored inside. > >On the other hand, the first example creates a single >workers field which is attached to the *class* itself, >not to a single instance. > >Given the names you've used, it seems likely the latter >example is the way you want to go (each hive has its own >workers). > >-Peter I think understand now... In Python, the first `workers` is a static/class member (java/delphi). However, unlike both of those, the class member is *also* accessible in objects of that type, not just in the static class type itself. eg. THive.workers (delphi) && HiveClass.workers (java) vs. hive().workers || x.workers || hive.workers || self.workers This is unusual in my experience... So, if an object writes to a member with the same name as a class member, does it change that class member for the class, all instances of the class, all future instances, or just self? ...or all of the above? Also, hive().workers isn't the same as hive.workers by any chance is it? I would assume it is not, but I can never tell with Python.
- Previous message (by thread): __init__ & attributes clarification
- Next message (by thread): __init__ & attributes clarification
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list