Search code examples
pythonreflectionintrospectionstatic-members

Python: Getting static properties via a property name


I have a python class with "emulated" static properties via a metaclass:

class MyMeta(type):
   @property
   def x(self): return 'abc'

   @property
   def y(self): return 'xyz'


class My: __metaclass__ = MyMeta

Now some of my functions receives the property name as a string, which should be retrieved from My.

def property_value(name):
   return My.???how to call property specified in name???

The point here is that I don't want an instance of My to be created.

Many thanks,

Ovanes


Solution

  • You could use

    getattr(My,name)