Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Class v. Instance variables in Python

Reply
Thread Tools

Re: Class v. Instance variables in Python

 
 
Joe Strout
Guest
Posts: n/a
 
      11-10-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is difference between Class variables and Instance variables? rahul8143@gmail.com Java 10 06-06-2011 06:43 AM
Class variables, instance variables, singleton; Ruby v. C++ Ralph Shnelvar Ruby 29 11-30-2009 07:43 PM
question about class variables and instance variables Eric D. Ruby 3 02-01-2006 07:57 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57