im trying to access a class method from a user input. An example would be that i want to get access the method "drive_to(destination)" from the instance "fast_bycicle" of the class "Bicycle". I want to let the user decide which bike to use so my idea kinda looks like this
class Bycicle():
def __init__(self, color, position)
self.color = color
self.position = position
def drive_to(self, destination)
self.position = destination
fast_bycicle = Bycicle("red","home")
x = input("Please select a bike: ")
x = "fast_bycicle"
y = input("Please select a destination: ")
y = "Hamburg"
x.drive_to(y)
which falls short. On the other hand side id(x) returns the id of the object.
I thought about creating a list and saving the bikes names/ids to a list and access them like that but i will run into the same problem that i cant access the instance by using a string. (I might be wrong on this)
Bycicleinstances.{("red", "home"): <object of Bycicle>}, and then, once you get the 2 inputs, invoke thedrive_to()of that object?dict. So your code should be like :mydict = {"fast_bycicle":Bycicle("red", "home")} mydict[x].drive_to(y)