0

If suppose i have a class name as a string, how can i use reflection to invoke its static member known before-hand? Some like this:

someInspectionMechanism("FooClass").staticMethod()
badmaash
  • 4,775
  • 7
  • 46
  • 61

1 Answers1

1

Use locals() or globals() to get the namespace dictionary, and look for a class with the name you want. The fact that you're using a static method is not relevant.

Example:

class Test:
    @staticmethod
    def method():
        return 'called static method'

assert locals()['Test'].method() == 'called static method'
Francis Avila
  • 31,233
  • 6
  • 58
  • 96