2
class One:
    i = One.get(9)
    @staticmethod
    def get(val):
        pass

I try to initialise the static variable using a static method, but the above code raises this error:

NameError: name 'One' is not defined

How can I initialise a static variable using a static method in Python?

martineau
  • 112,593
  • 23
  • 157
  • 280
chenzhongpu
  • 5,412
  • 7
  • 38
  • 70

1 Answers1

2
class One:
    @staticmethod
    def get(val):
        pass

    i = get.__func__(9)

Probably not the most pythonic way though. Note that the i variable is after the declaration of get. Since @staticmethod isn't callable directly (you'll receive a message if you do), you'd have to execute the underlaying function instead (__func__).

Caramiriel
  • 6,631
  • 3
  • 29
  • 49