close

[Solved] ‘staticmethod’ object is not callable

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ‘staticmethod’ object is not callable in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ‘staticmethod’ object is not callable Error Occurs?

Today I get the following error ‘staticmethod’ object is not callable in python.

How To Solve ‘staticmethod’ object is not callable this Error ?

  1. How To Solve 'staticmethod' object is not callable this Error ?

    To Solve 'staticmethod' object is not callable this Error Either create the dictionary after creating the class (so you access them as attributes), or bind explicitly, or extract the original function before storing them in the dictionary.

  2. 'staticmethod' object is not callable

    To Solve 'staticmethod' object is not callable this Error Either create the dictionary after creating the class (so you access them as attributes), or bind explicitly, or extract the original function before storing them in the dictionary.

Solution 1

You are storing unbound staticmethod objects in a dictionary. Such objects (as well as classmethod objects, functions and property objects) are only bound through the descriptor protocol, by accessing the name as an attribute on the class or an instance. Directly accessing the staticmethod objects in the class body is not an attribute access.

Either create the dictionary after creating the class (so you access them as attributes), or bind explicitly, or extract the original function before storing them in the dictionary.

Note that ‘binding’ for staticmethod objects merely means that the context is merely ignored; a bound staticmethod returns the underlying function unchanged.

So your options are to unindent the dictionary and trigger the descriptor protocol by using attributes:

class A(object):
    @staticmethod
    def open():
        return 123
    @staticmethod
    def proccess():
        return 456

A.switch = {
    1: A.open,
    2: A.proccess,   
}

or to bind explicitly, passing in a dummy context (which will be ignored anyway):

class A(object):
    @staticmethod
    def open():
        return 123
    @staticmethod
    def proccess():
        return 456

    switch = {
        1: open.__get__(object),
        2: proccess.__get__(object),   
    }

or access the underlying function directly with the __func__ attribute:

class A(object):
    @staticmethod
    def open():
        return 123
    @staticmethod
    def proccess():
        return 456

    switch = {
        1: open.__func__,
        2: proccess.__func__,   
    }

However, if all you are trying to do is provide a namespace for a bunch of functions, then you should not use a class object in the first place. Put the functions in a module. That way you don’t have to use staticmethod decorators in the first place and don’t have to unwrap them again.

Solution 2

In addition to Pieters’ answer, you can just drop @staticmethod:

class A(object):
    def open():
        return 123

    def proccess():
        return 456

    switch = {
        1: open,
        2: proccess,   
        }

obj = A.switch[1]()

However, in this way it becomes impossible to call open and process with self.

  • Outside the class they can be called either with A.open() and A.process(), just like common static methods.
  • Inside the class they can be called with just open() and process(), without A.. However,
    • A.open will fail. (I tested this case only with the decorator use. (@open))
    • What’s more, they must be placed before the function calling them.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read