Instances incorrectly sharing instance variables? 
Author Message
 Instances incorrectly sharing instance variables?

Hello,

I have a small problem trying to create a recursive data structure in
Python. I want to define a class which contains a list intended to
contain instances of the same class. Unfortunately, I am having a
problem with separate instances sharing instance variables. I do not see
what I am doing wrong here. I am sure it is something very simple.

The following is my class Entry:

class Entry:
        name = ''
        children = []
        def add(self, entry):
                self.children.append(entry)
        def __init__(self, name, children = []):
                self.name = name
                self.children = children
        def __repr__(self):
                s =  self.name + ':\n'
                for entry in self.children:
                        s = s + '\t' + entry.name + '\n'
                return s

Here is an interactive session using it.

Quote:
>>> from entry import *
>>> x = Entry('top')
>>> x                  # Top has no sub-entries

top:

Quote:
>>> x.add(Entry('item'))
>>> x                  # sub-entry 'item' is visible

top:
        item

Quote:
>>> x.children[0]      # unfortunately 'item' contains itself

item:
        item

Quote:
>>> x.children[0].add(Entry('bottom')) # add an entry to 'item'
>>> x                  # ack! all instances of Entry are sharing

top:                    # instance variables it seems.
        item
        bottom

If 'top' contains 'item'. Then if they are truly sharing instance
variables then item would be wiped out when I add 'bottom' because the
constructor initializes children to []. Why then does 'bottom' appear
under 'top' then?

Thank you for any help.

Thomas



Mon, 09 Aug 1999 03:00:00 GMT  
 Instances incorrectly sharing instance variables?

Quote:

> I have a small problem trying to create a recursive data structure in
> Python. I want to define a class which contains a list intended to
> contain instances of the same class. Unfortunately, I am having a
> problem with separate instances sharing instance variables. I do not see
> what I am doing wrong here. I am sure it is something very simple.

> The following is my class Entry:

> class Entry:
>         name = ''
>         children = []
>         def add(self, entry):
>                 self.children.append(entry)
>         def __init__(self, name, children = []):
>                 self.name = name
>                 self.children = children
>         def __repr__(self):
>                 s =  self.name + ':\n'
>                 for entry in self.children:
>                         s = s + '\t' + entry.name + '\n'
>                 return s

> Here is an interactive session using it.
> ...

This is a problem that many people new to python run into (the answer
should go in the FAQ :-).

The problem is the children=[] in your definition of __init__.  What is
happening is that an empty list object is created and used as the
default value for that argument... for ALL calls to it.  You then set
self.children to reference that same list object for any instance that
you create where you don't specify a "children" argument.  add() then
adds to that same list object.

[ I hope I was clear with that explanation... ]

Here is the "typical" way to solve the problem:

def __init__(self, name, children=None):
  if children is None:
    children = []
  self.name = name
  self.children = children

The above code will create a new list for each call rather than sharing
a list across them all.

Hope that helps!

-g

--



Mon, 09 Aug 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. instances of classes and instances of instances

2. Can event from instance propagate to other instances?

3. range(Instance) vs. foo[Instance]

4. Convert single instance tcl program to multiple instances...

5. Sharing VISA Resources With Multiple Application Instances

6. Shared Generic Instance Code

7. unintentionaly shared attributes in two different instances

8. how to instance shared objects ?

9. Multiple instance shared array bug?

10. Shared Instances

11. Getting shared slot without instance

12. Sharing code between instance and class methods

 

 
Powered by phpBB® Forum Software