Scott David Daniels wrote: > r0g wrote: >> ... >> A class is like a template which combines a complex data type (made from >> a combination of other data types) and the methods that operate on that >> data type. >> >> You generally don't work with classes directly but you make instances of >> them, each instance has it's own internal state and methods, initially >> these are the same as the templates but can be changed or overridden >> without affecting the state of any other instances you might have. > Take the tutorial and do experiments. > The attribute lookup checks the class _and_ the instance (with the > instance over-riding the class). Make sure you can explain the output > from this: > > class Demo(object): > non_template = 43 > > d = Demo() > print d.non_template > Demo.non_template = 44 > print d.non_template > d.non_template = 45 > print d.non_template > Demo.non_template = 46 > print d.non_template > > Once you can do that, explain this: > class Demo2(object): > holder = [] > > e = Demo2() > print e.holder > Demo2.holder.append(44) > print e.holder > e.holder.append(45) > print e.holder > Demo2.holder.append(46) > print e.holder > > # clue: > print d.holder is Demo.holder >
Well that all makes sense what with Pythons' 'only copy explicity' and lists being mutable and indeed, as you intuited, I did run into issues with this within days of starting to learn Python! Ever since I have been using copy.deepcopy() when I need to get the results described above i.e. to ensure each instance is fully independent of all the others. I hadn't really appreciated the consequences of this till now though e.g. that an instance might do a = a + 1 without affecting it's siblings but that b.append("fish") would affect b for everyone. I don't know if I will find any uses for that kind of behaviour but it doesn't hurt to understand it :-) Isn't Python's behaviour a little peculiar in this respect though, compared to classes in other languages? i.e. Are instances in other OO languages like Smalltalk, C++ fully independent copies or do their attribute names just point to one common object until reassigned like in python? (Or have I still not it at all?!) >> Is this correct enough for me to avoid the aforementioned bug pile? >> >> Also then, what _is_ an "instance variable" ? > > Well, when you use the term variable, I suspect that you think it > represents storage. OK, it kind of does, but only in the sense Well you used the term "instance variable" in your reply so I was just asking if there _is_ an actual thing known as "an instance variable" in Python and if so what it is/does. Thanks for your reply and examples BTW (I think) they are helping me clarify my understanding of python classes and the language used to describe them. Roger. > --Scott David Daniels > [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list