strange list behavior

Ken Seehof 12klat at sightreader.com
Tue Jun 6 23:40:07 EDT 2000


You should include enough relevant code and example usage to show the problem, in
particular the code where something is being assigned the value 'bar' and 'baz' would be
helpful.  Otherwise there is no way to guess what might be going wrong.

Perhaps this may give you some insight though:

>>> a = [1,2,3]
>>> b = a
>>> b[2] = 42
>>> b
[1, 2, 42]
>>> a
[1, 2, 42]

Remember that assignment is by reference, not by value.

Stefan Seefeld wrote:

> 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...

--
Ken Seehof
kens at sightreader.com
starship.python.net/crew/seehof
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000606/3297aedc/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 12klat.vcf
Type: text/x-vcard
Size: 343 bytes
Desc: Card for Ken Seehof
URL: <http://mail.python.org/pipermail/python-list/attachments/20000606/3297aedc/attachment.vcf>


More information about the Python-list mailing list