Call Class Method From Another Class

后端 未结 5 1339
耶瑟儿~
耶瑟儿~ 2020-11-28 05:27

In Python, is there a way to call a class method from another class? I am attempting to spin my own MVC framework in Python and I can not figure out how to invoke a method f

5条回答
  •  無奈伤痛
    2020-11-28 05:44

    Just call it and supply self

    class A:
        def m(self, x, y):
            print(x+y)
    
    class B:
        def call_a(self):
            A.m(self, 1, 2)
    
    b = B()
    b.call_a()
    

    output: 3

提交回复
热议问题