Calling a class method

Xavier Ho contact at xavierho.com
Sat Apr 17 09:14:16 EDT 2010


On Sat, Apr 17, 2010 at 11:09 PM, vsoler <vicente.soler at gmail.com> 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)
>

You're calling duplica with the class, and not by its object (self).

self.duplica()

will do what you want.

Cheers,
Xav


>
> 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?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100417/6d29b4ad/attachment-0001.html>


More information about the Python-list mailing list