Prev: when should I explicitly close a file?
Next: Can someone please make it more pythonic or better?
From: Walter Brameld IV on 17 Apr 2010 10:26 vsoler wrote: > I have the following script: > > class TTT(object): > def duplica(self): > self.data *= 2 > def __init__(self, data): > self.data = data > TTT.duplica(self.data) > def __str__(self): > return str(self.data) > > obj=TTT(7) > print obj > > And I want 14 printed (twice 7) > > I got the following error: > TypeError: unbound method duplica() must be called with TTT instance > as first argument (got int instance instead) > > What am I doing wrong? duplica() takes a 'self' parameter (a TTT instance), but you're calling it with self.data (an int instance). Change this line: TTT.duplica(self.data) to this: TTT.duplica(self) and it will do what you want.
From: Walter Brameld IV on 17 Apr 2010 10:26 vsoler wrote: > I have the following script: > > class TTT(object): > def duplica(self): > self.data *= 2 > def __init__(self, data): > self.data = data > TTT.duplica(self.data) > def __str__(self): > return str(self.data) > > obj=TTT(7) > print obj > > And I want 14 printed (twice 7) > > I got the following error: > TypeError: unbound method duplica() must be called with TTT instance > as first argument (got int instance instead) > > What am I doing wrong? duplica() takes a 'self' parameter (a TTT instance), but you're calling it with self.data (an int instance). Change this line: TTT.duplica(self.data) to this: TTT.duplica(self) and it will do what you want.
First
|
Prev
|
Pages: 1 2 Prev: when should I explicitly close a file? Next: Can someone please make it more pythonic or better? |