1

我正在尝试使用下面的代码动态设置方法来模拟数字对象。但相反,每个方法都设置为循环中的最后一个。为什么会发生这种情况,我该如何说服 Python 使用 DWIM?

#!/usr/bin/env python3.5
class Foo(float):
    pass

for tp in ("add", "sub", "mul", "truediv", "floordiv", "mod", "divmod", "pow"):
    methname = "__{:s}__".format(tp)
    print("defining", methname)
    def func(self, other):
        x = getattr(super(Foo, self), methname)(other)
        print("I calculated x={:.3f}!".format(x))
        return x
    func.__name__ = methname
    setattr(Foo, methname, func)

if __name__ == "__main__": # test
    print("Addition", Foo(3) + Foo(3))
    print("Multiplication", Foo(3) * Foo(3))

$ ./dyn.py
defining __add__
defining __sub__
defining __mul__
defining __truediv__
defining __floordiv__
defining __mod__
defining __divmod__
defining __pow__
I calculated x=27.000!
Addition 27.0
I calculated x=27.000!
Multiplication 27.0
4

1 回答 1

1

结果 27.0 是由 3**3 计算出来的,它是调用最后一次循环改变的 methname 调用时实际存储的函数。

func 中使用的变量引用了全局变量。尝试将 func 放入另一个方法并将循环变量作为参数传递,因此包含 func 的函数中的局部变量现在是恒定的,不会被循环更改。

#create function with the given name
# \return the function
def makeFunc(tp):
    methname = "__{:s}__".format(tp)
    print("defining", methname)
    def func(self, other):
        x = getattr(super(Foo, self), methname)(other)
        print("I calculated x={:.3f}!".format(x))
        return x
    func.__name__ = methname
    return func


for tp in ("add", "sub", "mul", "truediv", "floordiv", "mod", "divmod", "pow"):
    func=makeFunc(tp)
    setattr(Foo, func.__name__, func)
于 2017-02-23T20:04:12.103 回答