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
  • 119,623
  • 25
  • 170
  • 301
chenzhongpu
  • 6,193
  • 8
  • 41
  • 79

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
  • 7,029
  • 3
  • 30
  • 50