strange list behavior
Stefan Seefeld
seefelds at magellan.umontreal.ca
Tue Jun 6 14:50:16 EDT 2000
I'v run into a strange problem with lists. I'm not sure
at all where the problem is so I'll provide some context:
I'v a couple of classes holding string lists (which are scoped
names):
class Module:
def __init__(self, name): self.__name = name
#...
class Declarator:
def __init__(self, name): self.__name = name
#...
I create them from another object, which traverses (visits)
an AST:
class Visitor:
def visitDeclarator(self, node):
name = self.scope()
name.append(node.identifier())
self.__result_declarator = AST.Declarator(name)
def visitModule(self, node):
name = self.scope()
name.append(node.identifier())
module = AST.Module(name)
#...
what I *though* was happening is that I created temporary variables 'name'
in the respective scopes of all the visitSomething methods and then hand over
the objects to the newly created Node types (Module, Declarator, etc.)
However, what I *observe* is that even though a Module is initialized with
name : ['foo', 'bar']
it may end up being
name : ['foo', 'baz']
i.e. it seems the list is not properly released but instead modified from the outside.
printing out 'id(name)' reveals indeed that the various temporary 'name' objects
in the Visitor class have all the same id, i.e. they are the same object. Given
that the AST nodes are created with the list copied by reference, not by value,
I should not wonder that the value changes. Is this a fault of GC ?
Could anybody explain what's going on here and how I get the behavior I want ?
Thanks a lot !
Stefan
_______________________________________________________
Stefan Seefeld
Departement de Physique
Universite de Montreal
email: seefelds at magellan.umontreal.ca
_______________________________________________________
...ich hab' noch einen Koffer in Berlin...
More information about the Python-list
mailing list