85

Is there a way to call the method of a class from another class? I am looking for something like PHP's call_user_func_array(). Here is what I want to happen:

class A:
    def method1(arg1, arg2):
        ...

class B:
    A.method1(1, 2)
3
  • 2
    Does that really need to be a class method and not a function? Static methods in other languages don't necessarily map to a class method in Python. Give this a read: dirtsimple.org/2004/12/python-is-not-java.html
    – David Webb
    Commented Oct 4, 2010 at 15:09
  • 23
    @Ivo Honestly, what do you care if he writes his own MVC before he learns the basics? let him try and learn the basics in the process. quit being so condescending to people asking questions. Commented Oct 4, 2010 at 15:38
  • maybe duplicate to stackoverflow.com/questions/3311987/…
    – max
    Commented Dec 14, 2011 at 3:56

6 Answers 6

79

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.

2
  • 1
    what is significance of cls in @classmethod def method3(cls, a, b): return cls.method2(a, b) Commented Feb 6, 2018 at 4:39
  • The use of getattr here is utterly pointless. A.method1 is equivalent, simpler, and faster. getattr is only useful when the attribute name to access is determined at runtime. Commented Oct 3, 2018 at 17:18
20

Just call it and supply self:

class A:
    def method(self, x, y):
        print(x + y)

class B:
    def call_a(self):
        A.method(self, 1, 2)

b = B()
b.call_a()

Output:

3
5
  • 8
    this example is wrong as you are passing class B reference to class A method Commented Nov 21, 2017 at 16:59
  • 1
    I guess it will work as long as all attributes referred as self.x in the method also exist on B
    – a1an
    Commented Mar 6, 2018 at 15:46
  • 3
    @VarunMaurya, Python uses duck typing, so classes aren't checked. As a1an says, long as you supply an object with the correct attributes this will work.
    – ratiotile
    Commented Mar 7, 2018 at 22:50
  • @ratiotile I'm getting "TypeError: unbound method m() must be called with A instance as first argument (got B instance instead) "
    – oak
    Commented Apr 6, 2018 at 9:14
  • @oak, I don't know what version of python you are using, but the example was in Python3: repl.it/repls/DecentPurpleBsddaemon
    – ratiotile
    Commented Apr 7, 2018 at 14:30
6
class CurrentValue:

    def __init__(self, value):
        self.value = value

    def set_val(self, k):
        self.value = k

    def get_val(self):
        return self.value


class AddValue:

    def av(self, ocv):
        print('Before:', ocv.get_val())
        num = int(input('Enter number to add : '))
        nnum = num + ocv.get_val()
        ocv.set_val(nnum)
        print('After add :', ocv.get_val())


cvo = CurrentValue(5)

avo = AddValue()

avo.av(cvo)

We define 2 classes, CurrentValue and AddValue We define 3 methods in the first class One init in order to give to the instance variable self.value an initial value A set_val method where we set the self.value to a k A get_val method where we get the valuue of self.value We define one method in the second class A av method where we pass as parameter(ovc) an object of the first class We create an instance (cvo) of the first class We create an instance (avo) of the second class We call the method avo.av(cvo) of the second class and pass as an argument the object we have already created from the first class. So by this way I would like to show how it is possible to call a method of a class from another class.

I am sorry for any inconvenience. This will not happen again.

Before: 5

Enter number to add : 14

After add : 19

2
  • It would help if you added further explanation, so readers would know why your answer was a good one. Code only answers are not always clear to novice readers. Commented Oct 3, 2018 at 17:28
  • 4
    you got typo on "ocv.get_val()" . should it be cvo instead of ocv?
    – greendino
    Commented Aug 1, 2020 at 17:04
3

You can call a function from within a class with:

A().method1()

2
  • If A had already inherited from somewhere else (say "Red"), would I need to include the base class inside A? i.g. A(Red).method1()
    – masque
    Commented Aug 8, 2018 at 17:18
  • I know this is a bit late, but no, that is not required. Commented Feb 21, 2019 at 21:44
2

In Python function are first class citezens, so you can just assign it to a property like any other value. Here we are assigning the method of A's hello to a property on B. After __init__, hello will be attached to B as self.hello, which is actually a reference to A's hello:

class A:
    def hello(self, msg):
        print(f"Hello {msg}")
    
class B:
    hello = A.hello

print(A.hello)
print(B.hello)

b = B()
b.hello("good looking!")

Prints:

<function A.hello at 0x7fcce55b9e50>
<function A.hello at 0x7fcce55b9e50>
Hello good looking!
1
  • 1
    Why is this answer negatively rated? It seems like a cleaner way to reassign a method to another class
    – Andy K
    Commented Feb 25, 2023 at 16:51
0

Better you make an object of the required class then call that object.method instead of Class.method

Not the answer you're looking for? Browse other questions tagged or ask your own question.