Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'staticmethod' object is not callable

Tags:

python

I have this code:

class A(object):     @staticmethod     def open():         return 123     @staticmethod     def proccess():         return 456      switch = {         1: open,         2: proccess,            }  obj = A.switch[1]() 

When I run this I keep getting the error:

TypeError: 'staticmethod' object is not callable 

how to resolve it?

like image 456
Ramin Farajpour Cami Avatar asked Jan 29 '17 13:01

Ramin Farajpour Cami


People also ask

How do you call a Staticmethod function in Python?

If anything not using the decorator gives you a method that is more "static" because you can't even call it from an instance object. When you use @staticmethod you can call the method from both the class and class instance objects.

What does Staticmethod mean?

A static method (or static function) is a method defined as a member of an object but is accessible directly from an API object's constructor, rather than from an object instance created via the constructor.

How do you call a class with Staticmethod?

To call a static method in class use a static method from within the body of the class, and define the static method using the built-in staticmethod function as a decorator in Python.

How do you call a class in Staticmethod Python?

The static method works similarly to a function in a Python script, but it is located within the class body. A static method can be called from either a class or object reference. We can call it Utils if foo() is a static function in Class Utils. Utils.


1 Answers

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.

like image 147
Martijn Pieters Avatar answered Sep 28 '22 21:09

Martijn Pieters