Re: Class v. Instance variables in Python

Discussion in 'Python' started by Joe Strout, Nov 10, 2008.

  1. Joe Strout

    Joe Strout Guest

    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
    Joe Strout, Nov 10, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    35,874
    jporter892
    Jun 6, 2011
  2. Sridhar R
    Replies:
    14
    Views:
    1,398
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. Martin P. Hellwig
    Replies:
    1
    Views:
    374
    Martin P. Hellwig
    Mar 26, 2010
  4. Eric D.
    Replies:
    3
    Views:
    179
    Jeremy Henty
    Feb 1, 2006
  5. Ralph Shnelvar
    Replies:
    29
    Views:
    769
    David Masover
    Nov 30, 2009
Loading...

Share This Page