By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,598 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,598 IT Pros & Developers. It's quick & easy.

Re: Class v. Instance variables in Python

P: n/a
On Nov 10, 2008, at 2:44 PM, Zane Selvans wrote:
However, one (and only one) of these instance variables is behaving
mysteriously like a class variable: all instances of the class are
sharing a single copy of the variable, located at the same place in
memory.

Is there a common mistake that can result in this behavior, that
doesn't involve using class variables - I really have absolutely
nothing defined in the class except for methods. The problem shows
up regardless of how I name the instance variables (i.e. using name
mangling __names or not). The problematic variable consists of a
list of objects of type Lineament (the same object class as the one
that's giving me problems).
How are you creating your list? You need to make sure that each
instance creates its very own list object, something like:

self.foo = []

rather than grabbing a reference to some shared list instance, such as
one defined as a default argument to your __init__ method.

HTH,
- Joe

Nov 10 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.