Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling a class method

3 views
Skip to first unread message

vsoler

unread,
Apr 17, 2010, 9:09:21 AM4/17/10
to
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?

Andreas Waldenburger

unread,
Apr 17, 2010, 9:44:56 AM4/17/10
to
On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler
<vicent...@gmail.com> wrote:

> 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?

Not reading the error message.

You need to create a TTT instance and call your method from that:

inst = TTT()
inst.duplica(7)


I notice you ask a lot of very basic beginner questions. While there
is nothing wrong with being a beginner and asking questions, I think you
should read more introductory material and tutorials. Concerning
classes, pick one of the following that you like:

http://www.google.com/search?ie=UTF-8&q=python%20classes%20introduction
(Yes I am teasing you a bit ;) )

Also, maybe you'd like to post to the tutor list[1], which is (to my
understanding) intended for just this kind of question.

[1]: http://mail.python.org/mailman/listinfo/tutor

best
/W


--
INVALID? DE!

Andreas Waldenburger

unread,
Apr 17, 2010, 9:52:00 AM4/17/10
to
On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler
<vicent...@gmail.com> wrote:

> [snip actual question]

Oh and a note on vocabulary: A "class method" is a somewhat advanced
topic and quite probably not what you want here. They are not used very
often.

What I proposed in the other post was an "instance method", which is
usually what one means by the bare word "method". I think you should
familiarize yourself with this concept (that is, just plain Python
object oriented programming with classes and instances), before
delving into more arcane stuff such as class methods.

/W

--
INVALID? DE!

Andreas Waldenburger

unread,
Apr 17, 2010, 10:06:09 AM4/17/10
to
On Sat, 17 Apr 2010 15:44:56 +0200 Andreas Waldenburger
<use...@geekmail.INVALID> wrote:

> On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler
> <vicent...@gmail.com> wrote:
>
> > 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?
>
> Not reading the error message.
>

> [snip rest of my response]
>

No wait. I misread your code. Sorry.

There are two problems with this line:

TTT.duplica(self.data)

It should read

self.duplica()

Then it should work.

It may be a nice exercise for you to work out why the code needs to be
changed like that. On top of that, perhaps you'd like to think about
why you thought it should be "TTT.duplica(self.data)". Comparing that
to the correct way should be very beneficial to you understanding of
the matter. If you like you can post your thoughts here to verify them.

All my comments about reading tutorials and posting to python-tutor
still apply, however.

[Sidenote: What's that lone print statement above "obj=TTT(7)"
supposed to do?]

/W

--
INVALID? DE!

Steven D'Aprano

unread,
Apr 17, 2010, 10:16:17 AM4/17/10
to
On Sat, 17 Apr 2010 15:44:56 +0200, Andreas Waldenburger wrote:

> On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler
> <vicent...@gmail.com> wrote:
>
>> 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?
>
> Not reading the error message.
>
> You need to create a TTT instance and call your method from that:
>
> inst = TTT()
> inst.duplica(7)


He already has a TTT instance. Since he's calling the TTT.duplica method
from another TTT method, the easiest way (and the most Pythonic, and the
most sensible, is to do this:

self.duplica(7)

Calling duplica from the class as TTT.duplica will work, if he does:

TTT.duplica(self, 7)

but why would you want to?

--
Steven

Walter Brameld IV

unread,
Apr 17, 2010, 10:26:02 AM4/17/10
to

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.

Walter Brameld IV

unread,
Apr 17, 2010, 10:26:45 AM4/17/10
to

duplica() takes a 'self' parameter (a TTT instance), but you're calling

0 new messages