Is it bad practice to use python's getattr extensively?

后端 未结 2 561
闹比i
闹比i 2021-01-02 09:31

I\'m creating a shell-like environment. My original method of handleing user input was to use a dictionary mapping commands (strings) to methods of various classes, making u

2条回答
  •  忘掉有多难
    2021-01-02 10:24

    does getattr have the same problems as eval?

    No -- code using eval() is terribly annoying to maintain, and can have serious security problems. Calling getattr(x, "foo") is just another way to write x.foo.

    will I be taking a hit to the efficiency of my shell

    It will be imperceptibly slower if the command isn't found, but not enough to matter. You'd only notice it if doing benchmarks, with tens of thousands of entries.

提交回复
热议问题