[Python] Calling python dictionary of function from class

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I'm relatively new to python and would like to make a class that has a dictionary that corresponds to different methods in the class. I currently have :

    class Test:
    functions = {"Test1":test1, "Test2":test2}

    def test1(self, arg1):
    print "test1"

    def test2(self, arg1):
    print "test2" + arg1

    def call_me(self, arg):
    self.functions[arg](arg)


    Then in my main.py I have the following:

    from Test import Test

    t = Test()

    t.call_me('Test1')


    When I call this function I get an error saying name test1 is not defined. Can anyone tell me what I am doing wrong? Any help would be greatly appreciated.

    Continue reading...

Compartilhe esta Página