Calling a class method
Walter Brameld IV
wb at wbrameld4REMOVE_THIS_TEXT.name
Sat Apr 17 10:26:02 EDT 2010
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)
>
> print
> 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.
More information about the Python-list
mailing list