Let's assume I have a class, with a static method, and I want a class property to be set to the value that this method returns:
class A:
@staticmethod
def foo():
return 12
baz = foo()
But doing this I get an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in A
TypeError: 'staticmethod' object is not callable
I found a way to get around this:
class A:
class B:
@staticmethod
def foo():
return 2
baz = B.foo()
But for example if I write:
class A:
class B:
@staticmethod
def foo():
return 2
class C:
baz = B.foo()
I also get an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in A
File "<stdin>", line 7, in C
NameError: name 'B' is not defined
Is there a way to call static methods from within a class while declaring it? Why 1st and 3rd examples of code does not work but 2nd does? How python interpretor handles such declarations?
This is also a workaround, but it might help.
In the
class
body you cannot refer to the class being created. OTOH a class decorator receives an initialized class, thus it can call its methods directly.The
staticmethod
is a descriptor. A descriptor exposes the__get__(instance, cls)
method allowing it to be accessed either through an instance or at the class level.Now in your case you wish to call it within a class stanza. Normally this would not be possible as neither an instance nor the class are yet available. However a
staticmethod
ignores both in any case so you can use the following rather nasty approach to call it.Then
Note: The only reason to pass
object
as the second argument is thatstaticmethod
insists on being passed a class of some kind as the second argument.