How to access the method of another class in python?

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.

4 minute read Python 3.7—3.10

Watch as video

04:08

Sign in to your Python Morsels account to save your screencast settings.

Don't have an account yet? Sign up here.

How does class inheritance work in Python?

Creating a class that inherits from another class

We have a class called FancyCounter, that inherits from another class, Counter (which is in the collections module in the Python standard library):

from collections import Counter


class FancyCounter(Counter):
    def commonest(self):
        (value1, count1), (value2, count2) = self.most_common(2)
        if count1 == count2:
            raise ValueError("No unique most common value")
        return value1

The way we know we're inheriting from the Counter class because when we defined FancyCounter, just after the class name we put parentheses and wrote Counter inside them.

To create a class that inherits from another class, after the class name you'll put parentheses and then list any classes that your class inherits from.

In a function definition, parentheses after the function name represent arguments that the function accepts. In a class definition the parentheses after the class name instead represent the classes being inherited from.

Usually when practicing class inheritance in Python, we inherit from just one class. You can inherit from multiple classes (that's called multiple inheritance), but it's a little bit rare. We'll only discuss single-class inheritance right now.

Methods are inherited from parent classes

To use our FancyCounter class, we can call it (just like any other class):

>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter("Hello there!")

Our class will accept a string when we call it because the Counter class has implemented a __init__ method (an initializer method).

Our class also has a __repr__ method for a nice string representation:

>>> letters
FancyCounter({'e': 3, 'l': 2, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1})

It even has a bunch of other functionality too. For example, it has overridden what happens when you use square brackets to assign key-value pairs on class instances:

>>> letters['l'] = -2
>>> letters
FancyCounter({'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2})

We can assign key-value pairs because our parent class, Counter creates dictionary-like objects (a.k.a. mappings).

All of that functionality was inherited from the Counter class.

Adding new functionality while inheriting

So our FancyCounter class inherited all of the functionality that our Counter class has but we've also extended it by adding an additional method, commonest, which will give us the most common item in our class.

When we call the commonest method, we'll get the letter e (which occurs three times in the string we originally gave to our FancyCounter object):

>>> letters.commonest()
'e'

Our commonest method relies on the most_common method, which we didn't define but which our parent class, Counter, did define:

    def commonest(self):
        (value1, count1), (value2, count2) = self.most_common(2)
        if count1 == count2:
            raise ValueError("No unique most common value")
        return value1

Our FancyCounter class has a most_commonest method because our parent class, Counter defined it for us!

Overriding inherited methods

If we wanted to customize what happens when we assigned to a key-value pair in this class, we could do that by overriding the __setitem__ method. For example, let's make it so that if we assign a key to a negative value, it instead assigns it to 0.

Before when we assigned letters['l'] to -2, we'd like it to be set to 0 instead of -2 (it's -2 here because we haven't customized this yet):

>>> letters['l'] = -2
>>> letters['l']
-2

To customize this behavior we'll make a __setitem__ method that accepts self, key, and value because that's what __setitem__ is given by Python when it's called:

    def __setitem__(self, key, value):
        value = max(0, value)

The above __setitem__ method basically says: if value is negative, set it to 0.

If we stop writing our __setitem__ at this point, it wouldn't be very useful. In fact that __setitem__ method would do nothing at all: it wouldn't give an error, but it wouldn't actually do anything either!

In order to do something useful we need to call our parent class's __setitem__ method. We can call our parent class' __setitem__ method by using super.

    def __setitem__(self, key, value):
        value = max(0, value)
        return super().__setitem__(key, value)

We're calling super().__setitem__(key, value), which will call the __setitem__ method on our parent class (Counter) with key and our new non-negative value.

Here's a full implementation of this new version of our FancyCounter class:

from collections import Counter


class FancyCounter(Counter):
    def commonest(self):
        (value1, count1), (value2, count2) = self.most_common(2)
        if count1 == count2:
            raise ValueError("No unique most common value")
        return value1
    def __setitem__(self, key, value):
        value = max(0, value)
        return super().__setitem__(key, value)

To use this class we'll call it and pass in a string again:

>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter("Hello there!")

But this time, if we assign a key to a negative value, we'll see that it will be assigned to0 instead:

>>> letters['l'] = -2
>>> letters['l']
0

Summary

If you want to extend another class in Python, taking all of its functionality and adding more functionality to it, you can put some parentheses after your class name and then write the name of the class that you're inheriting from.

If you want to override any of the existing functionality in that class, you'll make a method with the same name as an existing method in your parent class. Usually (though not always) when overriding an existing method, you'll want to call super in order to extend the functionality of your parent class rather than completely overriding it.

Using super allows you to delegate back up to your parent class, so you can essentially wrap around the functionality that it has and tweak it a little bit for your own class's use.

That's the basics of class inheritance in Python.

Series: Classes

Classes are a way to bundle functionality and state together. The terms "type" and "class" are interchangeable: list, dict, tuple, int, str, set, and bool are all classes.

You'll certainly use quite a few classes in Python (remember types are classes) but you may not need to create your own often.

To track your progress on this Python Morsels topic trail, sign in or sign up.

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.

How do you access a method from another class?

To class a method of another class, we need to have the object of that class. Here, we have a class Student that has a method getName() . We access this method from the second class SimpleTesting by using the object of the Student class.

How do I call a class method from another file in Python?

Python modules can get access to code from another module by importing the file/function using import. The import statement is that the commonest way of invoking the import machinery, but it's not the sole way. The import statement consists of the import keyword alongside the name of the module.

How do I access a variable of one method inside another method in the same class in Python?

The variables that are defined inside the methods can be accessed within that method only by simply using the variable name. Example – var_name. If you want to use that variable outside the method or class, you have to declared that variable as a global. def access_method( self ):

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.