Calling a class method

V

vsoler

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

Andreas Waldenburger

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 classes introduction
(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
 
A

Andreas Waldenburger

[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
 
A

Andreas Waldenburger

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
 
S

Steven D'Aprano

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

Walter Brameld IV

vsoler said:
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.
 
W

Walter Brameld IV

vsoler said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top