Data sticking around too long
CedricCicada at gmail.com
CedricCicada at gmail.com
Wed Sep 6 16:57:51 EDT 2006
Greetings again!
There's something more to determining whether a class member is a class
variable or an instance variable. Here's a slightly expanded version
of my last script:
class ScannerCommand:
taskName = ''
scanList = []
number = 0
def __init__(self, data):
pass
# self.scanList = []
# self.scanList.append(data)
if __name__ == '__main__':
c1 = ScannerCommand("c1")
c2 = ScannerCommand("c2")
c1.number = 1
c2.number = 2
c1.scanList.append("One")
c2.scanList.append("Two")
print "C1: " + str(c1.number)
for data in c1.scanList:
print " " + data
print "C2: " + str(c2.number)
for data in c2.scanList:
print " " + data
And here's the output:
C1: 1
One
Two
C2: 2
One
Two
Here, the two instances share the scanList list, as expected from
previous discussions, but they have their own copies of the number
variable. Why is scanList a class variable but number an instance
variable here?
Good night, all!
Rob Richardson
RAD-CON, Inc.
Bay Village, OH
More information about the Python-list
mailing list