Home>

A question about python classes.

class test:
    def func1 (self):
        print ("Hello")
    def func2 (self):
        func1 ()
        print (", world")
test = test ()
test.func2 ()

In this code, when I try to call func1 with func2, I get an undefined error. How can I use it?
Sorry for the beginner's question.

  • Answer # 1

    If you want to access your own member method, be sure to add self anywhere.
    That is,

    def func2 (self):
        # Add self.
        self.func1 ()
        print (", world")


    It is OK if you modify it like this.

  • Answer # 2

    class Test:
        def func1 (self):
            print ("Hello")
        def func2 (self):
            self.func1 ()
            print (", world")
    test = Test ()
    test.func2 ()

    This is why self is essential for the first argument.