• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中实现结构相似的函数调用方法

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python调用函数,python如何调用函数,python怎么调用函数,python重复调用函数,python调用matlab函数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

python的dict用起来很方便,可以自定义key值,并通过下标访问,示例如下:
>>> d = {'key1':'value1',
... 'key2':'value2',
... 'key3':'value3'}
>>> print d['key2']
value2
>>>
</div>
lambda表达式也是很实用的东东,示例如下:
>>> f = lambda x : x**2
>>> print f(2)
4
>>>
</div>
两者结合可以实现结构相似的函数调用,使用起来很方便,示例如下:

示例一:不带参数 
#! /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]()
</div>

示例二:带参数

#! /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"
</div>

</div>

您可能想查找下面的文章:

  • python中私有函数调用方法解密
  • python中私有函数调用方法解密
  • Python中实现结构相似的函数调用方法

相关文章

  • Django的数据模型访问多对多键值的方法
  • Python的MongoDB模块PyMongo操作方法集锦
  • 解决Python传递中文参数的问题
  • Python中处理字符串之isalpha()方法的使用
  • 部署Python的框架下的web app的详细教程
  • 在Lighttpd服务器中运行Django应用的方法
  • Python中强大的命令行库click入门教程
  • Python and、or以及and-or语法总结
  • 基于Python实现通过微信搜索功能查看谁把你删除了
  • Python制作钉钉加密/解密工具

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python中的hashlib和base64加密模块使用实例
    • 如何运行Python程序的方法
    • win7上python2.7连接mysql数据库的方法
    • python将文本转换成图片输出的方法
    • python解析模块(ConfigParser)使用方法
    • 总结用Pdb库调试Python的方式及常用的命令
    • Python制作Windows系统服务
    • 各种Python库安装包下载地址与安装过程详细介绍(Windows版)
    • python友情链接检查方法
    • Python中Continue语句的用法的举例详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有