[Python] How to dynamically call function in class [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to call a particular class object's report() method based on user input (choice). How can I can do that, like choice.report()?

    class Ship:
    def __init__(self, name):
    self.name = name
    self.hold = {
    "water": {"quantity": 100, "price": 50},
    "coffee": {"quantity": 300, "price": 25}
    }

    def report(self):
    for key, value in self.hold.items():
    print(f"{key}_{value['quantity']} [Pcs]_{value['price']} [XMR]")

    sh1 = Ship("sh1")
    sh2 = Ship("sh2")

    sh2.hold["oranges"] = {"quantity": 100, "price": 10}

    choice = input("Report from sh1 or sh2? ")
    print(choice.report)


    I can not find answer via google maybe because i am addressing my problem wrongly.

    Continue reading...

Compartilhe esta Página