Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Class Method From Another Class

Tags:

python

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 from one class in another class.

Here is what I want to happen:

class A:     def method1(arg1, arg2):         # do code here  class B:     A.method1(1,2) 

I am slowly getting into Python from PHP so I am looking for the Python equivalent of PHP's call_user_func_array().

like image 990
Lark Avatar asked Oct 04 '10 14:10

Lark


People also ask

How do you call a class method from another class method in Python?

Call method from another class in a different class in Python. we can call the method of another class by using their class name and function with dot operator. then we can call method_A from class B by following way: class A: method_A(self): {} class B: method_B(self): A.

How do you call a method from another class without creating an object?

We can call a static method by using the ClassName. methodName. The best example of the static method is the main() method. It is called without creating the object.

How do you call a class inside another class?

Inner classesTo instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass. InnerClass innerObject = outerObject.


2 Answers

update: Just saw the reference to call_user_func_array in your post. that's different. use getattr to get the function object and then call it with your arguments

class A(object):     def method1(self, a, b, c):         # foo  method = A.method1 

method is now an actual function object. that you can call directly (functions are first class objects in python just like in PHP > 5.3) . But the considerations from below still apply. That is, the above example will blow up unless you decorate A.method1 with one of the two decorators discussed below, pass it an instance of A as the first argument or access the method on an instance of A.

a = A() method = a.method1 method(1, 2) 

You have three options for doing this

  1. Use an instance of A to call method1 (using two possible forms)
  2. apply the classmethod decorator to method1: you will no longer be able to reference self in method1 but you will get passed a cls instance in it's place which is A in this case.
  3. apply the staticmethod decorator to method1: you will no longer be able to reference self, or cls in staticmethod1 but you can hardcode references to A into it, though obviously, these references will be inherited by all subclasses of A unless they specifically override method1 and do not call super.

Some examples:

class Test1(object): # always inherit from object in 2.x. it's called new-style classes. look it up     def method1(self, a, b):         return a + b      @staticmethod     def method2(a, b):         return a + b      @classmethod     def method3(cls, a, b):         return cls.method2(a, b)  t = Test1()  # same as doing it in another class  Test1.method1(t, 1, 2) #form one of calling a method on an instance t.method1(1, 2)        # form two (the common one) essentially reduces to form one  Test1.method2(1, 2)  #the static method can be called with just arguments t.method2(1, 2)      # on an instance or the class  Test1.method3(1, 2)  # ditto for the class method. It will have access to the class t.method3(1, 2)      # that it's called on (the subclass if called on a subclass)                       # but will not have access to the instance it's called on                       # (if it is called on an instance) 

Note that in the same way that the name of the self variable is entirely up to you, so is the name of the cls variable but those are the customary values.

Now that you know how to do it, I would seriously think about if you want to do it. Often times, methods that are meant to be called unbound (without an instance) are better left as module level functions in python.

like image 166
aaronasterling Avatar answered Oct 18 '22 09:10

aaronasterling


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

like image 38
ratiotile Avatar answered Oct 18 '22 07:10

ratiotile