Python中实现结构相似的函数调用方法

WBOY
Release: 2016-06-10 15:17:31
Original
1303 people have browsed it

python的dict用起来很方便,可以自定义key值,并通过下标访问,示例如下:

复制代码 代码如下:

>>> d = {'key1':'value1',
... 'key2':'value2',
... 'key3':'value3'}
>>> print d['key2']
value2
>>>

lambda表达式也是很实用的东东,示例如下:
复制代码 代码如下:

>>> f = lambda x : x**2
>>> print f(2)
4
>>>

两者结合可以实现结构相似的函数调用,使用起来很方便,示例如下:

示例一:不带参数 

复制代码 代码如下:

#! /usr/bin/python
 
msgCtrl = "1 : pause\n2 : stop\n3 : restart\nother to quit\n"
 
ctrlMap = {
'1':    lambda : doPause(),
'2':    lambda : doStop(),
'3':    lambda : doRestart()}
 
def doPause():
        print 'do pause'
 
def doStop():
        print 'do stop'
 
def doRestart():
        print 'do restart'
 
if __name__ == '__main__':
        while True:
                print msgCtrl
                cmdCtrl = raw_input('Input : ')
                if not ctrlMap.has_key(cmdCtrl):break
                ctrlMap[cmdCtrl]()

示例二:带参数

复制代码 代码如下:

#! /usr/bin/python
 
msgCtrl = "1 : +\n2 : -\n3 : *\nother to quit\n"
 
ctrlMap = {
'1':    lambda x,y : x+y,
'2':    lambda x,y : x-y,
'3':    lambda x,y : x*y}
 
 
if __name__ == '__main__':
        while True:
                print msgCtrl
                cmdCtrl = raw_input('Input : ')
                if not ctrlMap.has_key(cmdCtrl):break
                print ctrlMap[cmdCtrl](10,2),"\n"
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template