How can I access a classmethod from inside a class in Python

前端 未结 6 1032
日久生厌
日久生厌 2020-12-03 06:40

I would like to create a class in Python that manages above all static members. These members should be initiliazed during definition of the class already. Due to the fact t

6条回答
  •  孤街浪徒
    2020-12-03 07:16

    You call a class method by appending the class name likewise:

    class.method
    

    In your code something like this should suffice:

    Test.static_init()
    

    You could also do this:

    static_init(Test)
    

    To call it inside your class, have your code do this:

    Test.static_init()
    

    My working code:

    class Test(object):
    
        @classmethod
        def static_method(cls):
            print("Hello")
    
        def another_method(self):
            Test.static_method()
    

    and Test().another_method() returns Hello

提交回复
热议问题