python实现部分实例

发现看着看着就思路就断了……于是打算用不同的语言实现一下书上的代码,理所当然地选用了Python,希望可以一定程度上缓解“看完了其实什么也不懂”的症状

看到复数部分所以就先实现这里啦

一开始的内容没什么好写的,从2.4.3数据导向的程序设计和可加性  开始,

(put <op> <type> <item>)

(get <op> <type>)

按照书中的描述很像字典(lisp应该是直接用列表+判断语句实现,效率不会差很多吗?)

于是

dict0 = {}
def put(_op, _type, _item):
    global dict0
    dict0[_op]= {_type:_item} 
def printout():
    print '123'
def get(_op, _type):
    global dict0
    if _op in dict0 and _type in dict0[_op]:
        return dict0[_op][_type]

out:

>>> put('a','b',printout)
>>> get('a','b')
<function printout at 0x02A319B0>
>>> get('a','b')()
123

成功啦哈哈

原文地址:https://www.cnblogs.com/autoria/p/4842835.html