Why can't I run this test class?

Kermit Mei kermit.mei at gmail.com
Fri Sep 11 09:30:42 CEST 2009


Dear all,
    I'm a newbie for python, and I write a program to test how to
implement a class:

#!/usr/bin/env
python                                                           

class Test:
    'My Test class'
    def __init__(self):
        self.arg1 = 1

    def first(self):
        return self.arg1

t1 = Test

print t1.first()


#################################

But when I run it, some errors take:

$ ./Test.py 
Traceback (most recent call last):
  File "./Test.py", line 13, in <module>
    print t1.first()
TypeError: unbound method first() must be called with Test instance as
first argument (got nothing instead)

What I want is as the following c++ code do:

class Test
{
public:
    Test()
    :arg1(1)
    {/*Do nothing*/}

int first() const
    { return arg1; }
protected:
    int arg1;
};

void main(void)
{
    Test t1;
    std::cout << t1.first;
}


Hope your help.
Thanks

Kermit





More information about the Python-list mailing list