[ python-Bugs-1549499 ] bug in classlevel variabels
SourceForge.net
noreply at sourceforge.net
Wed Aug 30 21:28:24 CEST 2006
Bugs item #1549499, was opened at 2006-08-30 18:44
Message generated for change (Comment added) made by gbrandl
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1549499&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: Python Interpreter Core
Group: Python 2.4
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Thomas Dybdahl Ahle (thomasda)
Assigned to: Nobody/Anonymous (nobody)
Summary: bug in classlevel variabels
Initial Comment:
class A:
js = []
def add (self, j):
self.js.append(j)
def clone (self):
c = A()
for j in self.js:
c.add(j)
return c
a = A()
b = a.clone()
b.add(3)
print a.js
print b.js
The above code should print "[]\n[3]", but instead it
prints "[3]\n[3]"!
It works as expected, if you change "js = []" to "def
__init__ (self):
self.js = []"
----------------------------------------------------------------------
>Comment By: Georg Brandl (gbrandl)
Date: 2006-08-30 19:28
Message:
Logged In: YES
user_id=849994
This is not a bug. The "js" list exists in the class's
namespace and exists only once, and if you don't rebind the
name, "self.js" always refers to this. Therefore it is
shared between instances.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1549499&group_id=5470
More information about the Python-bugs-list
mailing list