Prev: when should I explicitly close a file?
Next: Can someone please make it more pythonic or better?
From: vsoler on 17 Apr 2010 09:09 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?
From: Andreas Waldenburger on 17 Apr 2010 09:44 On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler <vicente.soler(a)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!
From: Andreas Waldenburger on 17 Apr 2010 09:52 On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler <vicente.soler(a)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!
From: Andreas Waldenburger on 17 Apr 2010 10:06 On Sat, 17 Apr 2010 15:44:56 +0200 Andreas Waldenburger <usenot(a)geekmail.INVALID> wrote: > On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler > <vicente.soler(a)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!
From: Steven D'Aprano on 17 Apr 2010 10:16 On Sat, 17 Apr 2010 15:44:56 +0200, Andreas Waldenburger wrote: > On Sat, 17 Apr 2010 06:09:21 -0700 (PDT) vsoler > <vicente.soler(a)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
|
Next
|
Last
Pages: 1 2 Prev: when should I explicitly close a file? Next: Can someone please make it more pythonic or better? |