python - How to retrieve a variable name given its value -
class type_name: def __init__(self, *field_names): self.x = field_names[0] self.y = field_names[1] self._fields = [x x in field_names] def get_y(self): return self.y def __getitem__(self, ind): fstr = 'self.get_' + str(self._fields[ind]) #this give me 5 want self.y construct string such 'self.get_y()' #and return eval on string return value
using code above,
point = pnamedtuple('point', ['x','y'], mutable = false) origin = point(0,5) print(origin[1])
it should work origin['y']
Comments
Post a Comment