python how to call static method from inside of a class body [duplicate]

我只是一个虾纸丫 提交于 2020-07-05 12:35:55

问题


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?


回答1:


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.

class A:
    @staticmethod
    def foo():
        return 12

    baz = foo.__get__(None, object)()

Then

>>> A.baz
12

Note: The only reason to pass object as the second argument is that staticmethod insists on being passed a class of some kind as the second argument.




回答2:


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.

def add_baz(cls):
    cls.baz = cls.foo()
    return cls 

@add_baz
class A:
    @staticmethod
    def foo():
        return 12


来源:https://stackoverflow.com/questions/45529384/python-how-to-call-static-method-from-inside-of-a-class-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!