Confounded by Python objects
wuwei23 at gmail.com
Thu Jul 24 12:03:09 CEST 2008
On Jul 24, 7:45 pm, "boblat... at googlemail.com"
<boblat... at googlemail.com> wrote:
> class Channel:
> name = ''
> sample = 
> def __init__(self, name):
> self.name = name
> def append(self, time, value):
> self.sample.append((time, value))
> def diag(self):
> print (self.name, self.sample)
Okay, the problem is you're appending to a _class_ attribute, not to
an instance attribute.
If you change your class definition to this, it should work:
def __init__(self, name):
self.name = name
self.sample = 
That will provide each instance with its own version of the sample
The 'self.name = name' in the __init__ for your original code binds a
new attribute to the instance, whereas 'self.sample.append(...' in the
class's append was appending to the class attribute instead.
Hope this helps.
More information about the Python-list