2

I'm working in Python and in a class method, I am trying to return an instance variable, but depending on the variable I want to return another instance variable:

def someFunction(self, variable):
    return self.variable

This is what I am trying to do, but the variable can have different names. This is in a class. How can I get this to work?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Noortje
  • 21
  • 1

1 Answers1

4

You are likely looking for attribute access:

def someFunction(self, variable):
    return getattr(self, variable)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135