开发者

Problem when using python xoltar toolkit, error raised when employ closure

Recently I've been reading some materials about functional programming in Python, one of them is here: http://www.ibm.com/developerworks/linux/library/l-prog2/index.html

I type the code:

from functional import *

taxdue        = lambda: (income-deduct)*rate
incomeClosure = lambda income,taxdue: closure(taxdue)
deductClosure = lambda deduct,taxdue: closure(taxdue)
rateClosure   = lambda rate,taxdue: closure(taxdue)

taxFP = taxdue
taxFP = incomeClosure(50000,taxFP)
taxFP = rateClosure(0.30,taxFP)
taxFP = deductClosure(10000,taxFP)

print"Functional taxes due =",taxFP()

print"Lisp-style taxes due =", \
      incomeClosure(50000,
          rateClosure(0.30,
              deductClosure(10000, taxdue)))()

but end up with following error information:

Functional taxes due =
Traceback (most recent call last):
  File "E:/Study开发者_JS百科/python/FP/FP1.py", line 16, in <module>
    print"Functional taxes due =",taxFP()
  File "E:/Study/python/FP/FP1.py", line 5, in <lambda>
    taxdue =        lambda: (income-deduct)*rate
NameError: global name 'income' is not defined

I'm using python 2.7.1, I want to know what the problem is, thank you in advance.


The problem is that the variables income, deduct and rate aren't defined anywhere when you do lambda: (income-deduct)*rate.

Maybe you intended to make them parameters to the lambda?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜