Typeerror: ‘classmethod’ object is not callable

For today’s discussion, we will walk you through fixing this “typeerror: ‘classmethod’ object is not callable.”

If you don’t have any idea how to fix this error, keep on reading!

In this article, we will hand you the solutions to this “‘classmethod’ object is not callable” error message.

What is “classmethod” object?

The “classmethod” object is used to define class method in Python. 


It is a built-in object that allows users to define a method as a class method that is bound to a class and not an instance of the class. 

It is created by using the @classmethod decorator above a method definition within a class. 

It is different from a regular method object in that it takes the class as its first argument rather than the instance of the class.

What is “typeerror: ‘classmethod’ object is not callable”?

The “typeerror: ‘classmethod’ object is not callable” is an error message that occurs when you are trying to call a “classmethod object” directly.

In a simple word, this error message occurs when you try to call a “classmethod” as if it were a regular method.

However, Python is treating it as an object of type “classmethod” instead.

A classmethod object is not callable, but the object returned by the classmethod’s get method is callable.

This means that you need to call the get method of the classmethod object to get a callable object.

What are the root causes of this error?

Here are the common root causes of the error:

👎 If you are using the incorrect usage of the “classmethod” decorator.

👎 Confusion between class methods and instance methods.

👎 When you mistakenly try to call a class method using an instance of the class rather than the class itself.

👎 When you accidentally over write the class method with a different object.

How to fix “”typeerror: ‘classmethod’ object is not callable”?”

To fix this error, ensure that you are calling the class method using the class name, and not an instance of the class.

You should also check that the class method is defined correctly, using the “@classmethod” decorator before the method definition.

Here are the different solutions you can use that can help you to get rid of this error:

1. Call the classmethod on the class itself

You can call the my_class_method “classmethod” directly on the MyClass class itself.

This code will work effectively and does not raise an error.

class MyClass:
    x = 100

    @classmethod
    def my_class_method(cls):
        cls.x += 10
        print(cls.x)

MyClass.my_class_method()

Output:

110

2. Call the classmethod on an instance of the class

In here, we create an instance of the MyClass class called my_instance and then call the my_class_method classmethod on this instance.

class MyClass:
    x = 110

    @classmethod
    def my_class_method(cls):
        cls.x += 10
        print(cls.x)

my_instance = MyClass()
my_instance.my_class_method()

Output:

120

3. Use the __get__method

You can also use the __get__method of the my_class_method classmethod object to get a callable object.

Then call the callable object.

class MyClass:
    x = 120

    @classmethod
    def my_class_method(cls):
        cls.x += 10
        print(cls.x)

callable_object = MyClass.my_class_method.__get__(MyClass)
callable_object()

Output:

130

4. Use the __func__attribute

Aside from using the __get__method you can also use the func attribute of the my_class_method classmethod object to get a reference to the underlying function.


Then call this function and pass in a reference to the MyClass class as its first argument.

class MyClass:
    x = 130

    @classmethod
    def my_class_method(cls):
        cls.x += 10
        print(cls.x)

MyClass.my_class_method.__func__(MyClass)

Output:

140

5. Use a staticmethod

Alternatively, you can also use a staticmethod instead of a classmethod.


A staticmethod is similar to a classmethod in that it is bound to the class and not an instance of the class.

class MyClass:
    x = 140

    @staticmethod
    def my_static_method():
        MyClass.x += 10
        print(MyClass.x)

MyClass.my_static_method()

However, it does not receive a reference to the class as its first argument.


Instead, you have to refer to the class explicitly by its name when accessing or modifying its attributes.

Output:

150

Conclusion

The “typeerror: ‘classmethod’ object is not callable” is an error message that occurs when you are trying to call a “classmethod object” directly.

This article already provides several solutions above so that you can fix the error message immediately.

We are hoping that this article provided you with sufficient solutions to get rid of the error.

You could also check out other “typeerror” articles that may help you in the future if you encounter them.