Solving ‘staticmethod’ object is not callable in Python

‘staticmethod’ object is not callable in Python is an error which occurs when you store unbound staticmethod objects in a dictionary.

In this article I am going to help you solve this error and understand the root of the problem, also I am presenting other possible solutions that may work if the main solution does not work for you.

Explaining the Error : ‘staticmethod’ object is not callable in Python

The problem happens when you store unbound staticmethod objects in a dictionary.

The error message should look like the error in the example bellow. Make sure it matches the error message in your case.

                                                                       #
TypeError: 'staticmethod' object is not callable
                                                                       #

Bellow is an example of code that will reproduce the error.

                                                                       #
class rabb(object):
    @staticmethod
    def open():
        return 434
    @staticmethod
    def proccess():
        return 232
    switch = {
        1: open,
        2: proccess,}
obj = rabb.switch[1]()
                                                                       #

Bellow I make my best attempt at solving the error and present the solution that may fix your problem.

Solution : Correctly store unbound staticmethod objects.

First, we should understand that in python, when you store unbound staticmethod objects in a dictionary, You can create the dictionary after creating the class and access them as attributes.

You can bind explicitly like in the example bellow.

                                                                       #
class rabb(object):
    @staticmethod
    def open():
        return 434
    @staticmethod
    def proccess():
        return 232
    switch = {
        1: open.__get__(object),
        2: proccess.__get__(object),}
                                                                       #

You can also use the func attribute to directly access the underlying function, Like in the example bellow.

                                                                       #
class rabb(object):
    @staticmethod
    def open():
        return 434
    @staticmethod
    def proccess():
        return 232
    switch = {
        1: open.__func__,
        2: proccess.__func__,}
                                                                       #

The two solutions above are how the error is solved in 90 percent of cases. And should work for you.

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

I hope the solutions have been helpful, I hope you solved the error already, you can support us by donating to our Kofi account, this website is free to use but any help is very appreciated and goes a long way.

This is the end of our article, I hope one of the above solutions was enough to solve the issue, make sure to keep learning and keep coding, Python is awesome and it is a skill worth learning, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/