K
KarlRixon
Given the following script, I'd expect p1.items to just contain
["foo"] and p2.items to contain ["bar"] but they both contain ["foo",
"bar"].
Why is this? Are object variables not specific to their instance?
---------------------------
#!/usr/bin/env python
class Parser:
items = []
def add_item(self, item):
self.items.append(item)
p1 = Parser()
p1.add_item("foo")
p2 = Parser()
p2.add_item("bar")
print p1
print p2
print p1.items
print p2.items
----------------------------
Output:
<__main__.Parser instance at 0x7fd812ccc098>
<__main__.Parser instance at 0x7fd812ccc0e0>
['foo', 'bar']
['foo', 'bar']
["foo"] and p2.items to contain ["bar"] but they both contain ["foo",
"bar"].
Why is this? Are object variables not specific to their instance?
---------------------------
#!/usr/bin/env python
class Parser:
items = []
def add_item(self, item):
self.items.append(item)
p1 = Parser()
p1.add_item("foo")
p2 = Parser()
p2.add_item("bar")
print p1
print p2
print p1.items
print p2.items
----------------------------
Output:
<__main__.Parser instance at 0x7fd812ccc098>
<__main__.Parser instance at 0x7fd812ccc0e0>
['foo', 'bar']
['foo', 'bar']