attribute assignment effects all class instances

anon anon at anon.net
Thu Sep 9 19:59:24 EDT 2004


I'm aware that you can assign a value to an attribute in all class
instances by assigning to <Class>.<attribute>, however, my case is
slightly different and bizarre.

In module node:

top, left, mid, right = range(4)

class Node:

   def __init__(self, ntype = None, function = None, symbol = None,     
      meta = None, adjacent = [None, None, None, None]):
      self.type = ntype
      self.function = function
      self.symbol = symbol
      self.meta = meta
      self.adjacent = adjacent


Here's the wierd thing, in another module I have:

node.adjacent[left] = y

where node is an instance of the Node class.  This statement assigns
all Node instances the value y to attribute adjacent[left].  This seems
very wrong, unless python resolves names in a case-insensitive way.

When I try the alternate:

node.adjacent = [None, y, None, None]

I get the behavior I expect (ie. only the instsance node gets its
attribute set)

Can anyone shed any light on this?

Thanks.
--Greg



More information about the Python-list mailing list