[ python-Bugs-1652387 ] Nested Objects scope problem

SourceForge.net noreply at sourceforge.net
Mon Feb 5 21:35:55 CET 2007


Bugs item #1652387, was opened at 2007-02-05 13:35
Message generated for change (Settings changed) made by kkelchev
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1652387&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Build
Group: Python 2.5
>Status: Open
Resolution: Invalid
Priority: 5
Private: No
Submitted By: kkelchev (kkelchev)
Assigned to: Nobody/Anonymous (nobody)
Summary: Nested Objects scope problem

Initial Comment:
K = MyObj()
K.Lines.append('First line to Obj "K"')
K.Lines.append('Second line to Obj "K"')
L = MyObj()
L.Lines.append('First line to Obj "L"')
print 'Lines from Obj "K"',K.Lines
print 'Lines from Obj "L"',L.Lines


Result is:
[Dbg]>>> 
Lines from Obj "K" ['First line to Obj "K"', 'Second line to Obj "K"', 'First line to Obj "L"']
Lines from Obj "L" ['First line to Obj "K"', 'Second line to Obj "K"', 'First line to Obj "L"']
>>> 

Why data appended into nested list filed “Lines”  into different object “K” and “L”  appears in both objects ?



----------------------------------------------------------------------

Comment By: kkelchev (kkelchev)
Date: 2007-02-05 20:34

Message:
Logged In: YES 
user_id=1711016
Originator: YES

Sorry :(
I forgot to paste definision of Class MyObj
yes it is:

class MyObj:
    Lines = []
--------------------------------------
is this a bug ? 
Or always in Python data in nested object are shared between all objects
from same class

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2007-02-05 18:31

Message:
Logged In: YES 
user_id=849994
Originator: NO

Sadly you didn't attach the definition of the class "MyObj".

I assume it is like this:

class MyObj:
    Lines = []

In this case, the Lines list object is shared by all instances of MyObj.
Please post to the python-list mailing list for further questions about
this behavior.

If your bug is different, please feel free to reopen this.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1652387&group_id=5470


More information about the Python-bugs-list mailing list