newbie: function-parameter

David Brown david at no.westcontrol.spam.com
Tue Nov 19 07:23:30 EST 2002


"Thomas Rademacher" <rademacher at prostep.FTA-berlin.de> wrote in message
news:ard9mk$hamtu$1 at ID-28938.news.dfncis.de...
> Hallo,
>
> I wrote a class with a function and I called the function:
>
> class Test:
>    def testfunc():
>      print 'test in class'
>
> mytest = Test()
> mytest.testfunc()
>
> Now I get the error message:
> "TypeError: testfunc() takes no arguments (1 given)"
>
> With "def testfunc(x):" my python script works fine.
> Why I need the parameter "x"??
>
> Thanks for your hints, Thomas
>

When you call the method "mytest.testfunc()", you are really calling the
function "testfunc" with a single parameter "mytest" - i.e.,
"testfunc(mytest)".
So the function needs to be defined to take that parameter.  By (pretty much
universal) convention, this first parameter is called "self" :

class Test:
    def testfunc(self) :
        print 'test in class'







More information about the Python-list mailing list