Confounded by Python objects
boblatest at googlemail.com
boblatest at googlemail.com
Thu Jul 24 05:45:04 EDT 2008
Hello group,
take a look at the code snippet below. What I want to do is initialize
two separate Channel objects and put some data in them. However,
although two different objects are in fact created (as can be seen
from the different names they spit out with the "diag()" method), the
data in the "sample" member is the same although I stick different
values in.
Thanks,
robert
(Sorry for Google Groups, but I don't have NNTP at work)
Here's the code:
#!/usr/bin/python
class Channel:
name = ''
sample = []
def __init__(self, name):
self.name = name
def append(self, time, value):
self.sample.append((time, value))
self.diag()
def diag(self):
print (self.name, self.sample)
chA = Channel('A')
chB = Channel('B')
chA.append(1, 1.1)
chB.append(2, 2.1)
chA.append(3, 1.2)
chB.append(4, 2.2)
print 'Result:'
chA.diag()
chB.diag()
------------------------------------
and here's the output:
('A', [(1, 1.1000000000000001)])
('B', [(1, 1.1000000000000001), (2, 2.1000000000000001)])
('A', [(1, 1.1000000000000001), (2, 2.1000000000000001), (3, 1.2)])
('B', [(1, 1.1000000000000001), (2, 2.1000000000000001), (3, 1.2), (4,
2.2000000000000002)])
Result:
('A', [(1, 1.1000000000000001), (2, 2.1000000000000001), (3, 1.2), (4,
2.2000000000000002)])
('B', [(1, 1.1000000000000001), (2, 2.1000000000000001), (3, 1.2), (4,
2.2000000000000002)])
What I'd like to see, however, is 2 tuples per Channel object, like
this:
('A', [(1, 1.1000000000000001)])
('B', [(2, 2.1000000000000001)])
('A', [(1, 1.1000000000000001), (3, 1.2)])
('B', [(2, 2.1000000000000001), (4, 2.2000000000000002)])
Result:
('A', [(1, 1.1000000000000001), (3, 1.2)])
('B', [(2, 2.1000000000000001), (4, 2.2000000000000002)])
More information about the Python-list
mailing list