简体   繁体   中英

Class method can not call other class methods within the same class

I have a class defined with two methods:

class A:
    def called():
        print 'called'
    def caller(self):
        called()

But caller can not use called directly

A().caller()

gives error

NameError: global name 'called' is not defined

How can I call the other unbounded method within the same class ?

Qualify the method with self or the class name A .

class A:

    @staticmethod
    def called():
        print 'called'

    def caller(self):
        self.called()
        # Or
        A.called()

NOTE I changed the method called as a static method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM