pythonstatic-methods

How can I call static method from static method without using class name?


Example of code: How i run this code without using 'A'

    class A:
        @staticmethod
        def a():
            print('a')
    
        @staticmethod
        def b():
            print('b')
            print(**A**.a())
    
    A.a()

Solution

  • you can refer the class with __class__.

    try this:

    class A:
        @staticmethod
        def a():
            print('a')
    
        @staticmethod
        def b():
            print('b')
            __class__.a() # notice here we're not using `A`
        
    A.b()