This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Nested Objects scope problem
Type: Stage:
Components: Build Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, kkelchev
Priority: normal Keywords:

Created on 2007-02-05 13:35 by kkelchev, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg31180 - (view) Author: kkelchev (kkelchev) Date: 2007-02-05 13:35
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 ?

msg31181 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-02-05 18:31
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.
msg31182 - (view) Author: kkelchev (kkelchev) Date: 2007-02-05 20:34
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
msg31183 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-02-05 20:54
Yes, this is exactly what I had written in my previous answer.
History
Date User Action Args
2022-04-11 14:56:22adminsetgithub: 44546
2007-02-05 13:35:20kkelchevcreate