我编写了一个程序,其中我有一个相当典型的课程。在这个类中,我创建了多个 namedtuple 对象。namedtuple 对象包含许多项目,它们都工作正常,除了我尝试绑定到它的 lambda 函数。下面是一个精简的示例和我收到的错误消息。希望有人知道为什么会出错。提前致谢!
文件:test.py
from equations import *
from collections import namedtuple
class Test:
def __init__(self, nr):
self.obj = self.create(nr)
print self.obj.name
print self.obj.f1(2)
def create(self, nr):
obj = namedtuple("struct", "name f1 f2")
obj.name = str(nr)
(obj.f1, obj.f2) = get_func(nr)
return obj
test = Test(1)
文件:方程.py
def get_func(nr):
return (lambda x: test1(x), lambda x: test2(x))
def test1(x):
return (x/1)
def test2(x):
return (x/2)
错误:
Traceback (most recent call last):
File "test.py", line 17, in <module>
test = Test(1)
File "test.py", line 8, in __init__
print self.obj.f1(2)
TypeError: unbound method <lambda>() must be called with struct instance as first argument (got int instance instead)`