Accessing a class instance with a variable

Accessing a class instance with a variable
0.0 0

#1

Is it possible to assign a class instance name to a variable, then call that variable to use a class method?

I have the following class:

class Grain(Ingredient):
    def __init__(self, name, extract, color):
        Ingredient.__init__(self, name)
        self.extract = extract
        self.color = color
   def get_extract(self):
        return self.extract

The following instance:

marisOtter = Grain("Maris Otter", 1.038, 5.0)

A library:

grainExtracts = []

And this code:

grainID = input("Enter grain name: ")
grainExtracts.append(grainID.get_extract())

If the use inputs “marisOtter”, I get the following error:

TypeError: 'str' object is not callable

I have tested other aspects of my code, for example if I input:

grainExtracts.append(marisOtter.get_extract())

it performs as expected and the output of grainExtracts is [1.038]
If I input “grainID” the output is “marisOtter”

What do I need to do for “grainID” to be recognized not as the string “grainID”, but as the instance name inputted by the user (in this case “marisOtter”)?

Or should I go about this completely differently?!?!


#2

not a safer option but you can try eval function to achieve your task …

Python eval