[Tutor] TypeError

Bob Gailer bgailer at alum.rpi.edu
Mon Aug 11 07:48:46 EDT 2003


At 02:39 PM 8/11/2003 +0200, just4info wrote:
>i´m programming in oo languages (java, ada) some years and now i 
>experimenting with python.
>general things are no problem, but oo is a litte bit tricky :-)
>
>i´ve two files: dummy.py an call.py
>
>in dummy.py there is a class Dummy -->
>
>class Dummy:
>
>     def call_me():
>         x = "Hello"
>         return x
>
>     def call_me_2():
>         return "Yes"
>
>an call.py imports the class -->
>
>import dummy
>
>you = dummy.Dummy()
>
>print "\n"
>print you.call_me()
>print "\n"
>print you.call_me_2()
>print "\n"
>
>in my eyes it should be correct syntax, but ich got the following error:
>
>Traceback (most recent call last):
>   File 
> "C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", 
> line 301, in RunScript
>     exec codeObject in __main__.__dict__
>   File "C:\Dokumente und Einstellungen\suess\Desktop\call.py", line 6, in ?
>     print you.call_me()
>TypeError: call_me() takes no arguments (1 given)
>
>i used pythonwin (activestate python 2.2).

Instance methods (call_me, call_me_2) are called with the instance as the 
first parameter. Thus the method definition must provide a parameter to 
receive the instance. Change:
     def call_me():
to:
     def call_me(self):
and likewise change:
     def call_me_2():
to:
     def call_me_2(self):

self is not a keyword, it is just customary Pythonese to use "self". How 
might one take advantage of this? Consider:
class Dummy:
     def save(self, value):
         self.x = value
     def recall(self):
         return self.x
import dummy

you = dummy.Dummy()
you.save("Green Stamps")
you2 = dummy.Dummy()
you2.save("Time in a bottle")
print you.recall(),you2.recall()

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003


More information about the Tutor mailing list