开发者

how to call to method A of class A from method B of class A

there seems to be a lot of answers on how to call method A of class A from class B but none on how to call to method A of class A from method B of class A. this probably easy but am new to oop in python. here is a quick illust开发者_开发问答ration of what am trying to do

  class A:
      def __init__():

      def method_A(self):
          xxxxxx
          return xxx...
      def method_B(self):
          ans = method_A(self)
          return ans

am getting the following error ======> "global name method_A ' is not defined"


ans = self.method_A()

self is an instance of A, and methods on instances are called using instance.method().


Attributes need to be referenced off the object.

ans = self.method_A()


Members of a class are accessed as attributes of self, so you just use this:

self.method_A()

Also, you do not need to pass self as parameter explicitly, that is done implicitly. This is all covered in the Python Tutorial, which everyone should read.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜