I am new to Python and while using it to write a small application I encountered a problem. As I can't find anything simliar on Google, I might have missunderstood some basic concepts of Python. It would be great if someone of you guys can explain that to me.
So here is a minimal working example. Please ignore the class and variable names, that is as said just an artificial example, but it shows what I mean. You can c&p it to your IDE and run it for yourself.
class State(object):
def __init__(self, wrappers = []):
self.wrappers = wrappers
def add(self, name, items):
lst = KeyValList(name)
for item in items:
lst.add(item, items[item])
self.wrappers.append(Wrapper(lst))
class Wrapper(object):
def __init__(self, kv_lst):
self.kv_lst = kv_lst
class KeyValList(object):
def __init__(self, name, kvals = []):
self.name = str(name)
self.kvals = kvals
def add(self, key, value):
self.kvals.append(KeyVal(key, value))
class KeyVal(object):
def __init__(self, key, val):
self.key = str(key)
self.val = val
Ok, that is the definition of the classes I use.
Right below I write the following:
state = State()
kv = { "key1" : "1", "key2" : "2", "key3" : "3" }
state.add("1st", kv)
kv = { "keyX" : "X", "keyY" : "Y", "keyZ" : "Z" }
state.add("2nd", kv)
for wrap in state.wrappers:
print wrap.kv_lst.name, "-->",
for kv in wrap.kv_lst.kvals:
print "%s:%s" % (kv.key, kv.val),
print ""
As a Java programmer I would expect the following output:
1st --> key3:3 key2:2 key1:1
2nd --> keyZ:Z keyY:Y keyX:X
However, with this program in Python I get the following output instead:
1st --> key3:3 key2:2 key1:1 keyZ:Z keyY:Y keyX:X
2nd --> key3:3 key2:2 key1:1 keyZ:Z keyY:Y keyX:X
Why do both lists contain all key/value pairs?
I expect the problem somewhere in the KeyValList
class, because while debugging when the state.add()
method is called in the main program for the 2nd time the kvals
parameter of the KeyValList
constructor contains the former pairs. But how can that be? I do not provide anything to this kvals
parameter, shouldn't it therefore be initialised to an empty list, due to the parameters default value?
PS: I am using Python 2.7.3 on Windows.