• + 16 comments

    And for the sake of wanting to learn... I decided to look up how to replicate the ability to call a function via a name stored in a variable. In PHP, it would be $func(var); but I found in Python, it is getattr(Object, Function)(Vars)

    Example of using this method below. Again, this is assuming the user isn't going to enter in an invalid command!

    T = int(raw_input().strip())
    L = []
    for t in range(T):
        args = raw_input().strip().split(" ")
        if args[0] == "print":
            print L
        elif len(args) == 3:
            getattr(L, args[0])(int(args[1]), int(args[2]))
        elif len(args) == 2:
            getattr(L, args[0])(int(args[1]))
        else:
            getattr(L, args[0])()