如何替换使用eval调用类?

2024-06-25 22:48:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道eval不好,我对编码很陌生

我使用配置解析器来定义打开输出门以转移部件的马达

从我的配置文件:

 [Motor]out1=["Screw", "board0.motor1"]

代码

    out1=json.loads(config['Motor']['out1'])
    board0 = MotorKit(address= int(config['Motor']['board0'], 16))
    def openMotor(m):
        eval(m).throttle = 1
        time.sleep(0.1)
        eval(m).throttle = 0.2
    
    def open_gate(part):
        if part == out1[0]:
            openMotor(out1[1])
    open_gate("screw")

我如何不使用eval?但是能够在openMotor函数中设置属性board0.motor1.throttle


Tags: config解析器编码defevalopenthrottlepart