Python 函数名作为字典值
Python中是没有switch的, 所以有时我们需要用switch的用法, 就只能通过if else来实现了. 但if else写起来比较冗长,
这时就可以使用Python中的dict来实现, 比switch还要简洁. 用法如下:
如果是key1的情况就执行func1, 如果是key2的情况就执行func2...(func1, func2...所有的函数的参数形式需要相同),
假设各个函数参数均为(arg1, arg2):
dictName = {"key1":func1, "key2":func2, "key3":func3"...} #字典的值直接是函数的名字,不能加引号
dictName[key](arg1, arg2)
示例代码如下:
#!/usr/bin/python
#File: switchDict.py
#Author: lxw
#Time: 2014/10/05 import re def add(x, y):
return x + y def sub(x, y):
return x - y def mul(x, y):
return x * y def div(x, y):
return x / y def main():
inStr = raw_input("Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.\n")
inList = re.split("(\W+)", inStr)
inList[1] = inList[1].strip()
print("-------------------------")
print(inList)
print("-------------------------") #Method 1:
if inList[1] == "+":
print(add(int(inList[0]), int(inList[2])))
elif inList[1] == "-":
print(sub(int(inList[0]), int(inList[2])))
elif inList[1] == "*":
print(mul(int(inList[0]), int(inList[2])))
elif inList[1] == "/":
print(div(int(inList[0]), int(inList[2])))
else:
pass #Method 2:
try:
operator = {"+":add, "-":sub, "*":mul, "/":div}
print(operator[inList[1]](int(inList[0]), int(inList[2])))
except KeyError:
pass if __name__ == '__main__':
main()
Output:
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
1 + 2
-------------------------
['', '+', '']
-------------------------
3
3
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
4 - 9
-------------------------
['', '-', '']
-------------------------
-5
-5
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
6 / 5
-------------------------
['', '/', '']
-------------------------
1
1
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
1 9 9
-------------------------
['', '', '', ' ', '']
-------------------------
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
1 ( 9
-------------------------
['', '(', '']
-------------------------
PS J:\>
个人感觉, 如果想用switch来解决某个问题, 并且每种情况下的操作在形式上是相同的(如都执行某个函数并且这些函数有
相同的参数), 就可以用这种方法来实现.
Python 函数名作为字典值的更多相关文章
- python函数2(返回值、传递列表...)
python函数2(返回值.传递列表...) 1.返回值 1.1.返回简单的值 #返回简单值 def get_formatted_name(first_name,last_name): "& ...
- 9 - Python函数定义-位置参数-返回值
目录 1 函数介绍 1.1 为什么要使用函数 1.2 Python中的函数 2 函数的基本使用 3 函数的参数 3.1 参数的默认值 3.2 可变参数 3.2.1 可变位置传参 3.2.2 可变关键字 ...
- python函数基础-参数-返回值-注释-01
什么是函数 函数就是有特定功能的工具 # python中有内置函数(python解释器预先封装好的)与自定义函数(用户自定义封装的)之分 为什么要用函数 # 可以减少代码冗余,增加代码复用性 # 使代 ...
- Python函数变量和返回值
Python函数的全局变量和局部变量 1.不同的编程语言,程序可以分为函数和过程两大类,函数具有具体返回值,而过程则不具有具体的返回值,python只具有函数,因为对于它的一般函数,其返回值为所具体返 ...
- python函数1_参数,返回值和嵌套
函数 将重复的代码,封装到函数,只要使用直接找函数 函数可以增强代码的模块化和提高代码的重复利用率 函数的定义和调用 格式 def 函数名([参数,参数...]): 函数体 定义函数 import r ...
- Python函数(一)-return返回值
定义一个函数可以在最后加上return返回值,方便查看函数是否运行完成和返回函数的值 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR&qu ...
- python 函数返回多个值
参考文献:http://blog.csdn.net/facevoid/article/details/5369146
- python函数,定义,参数,返回值
python中可以将某些具备一定功能的代码写成一个函数,通过函数可以在一定程度上减少代码的冗余,节约书写代码的时间.因为有一些代码实现的功能我们可能会在很多地方用到. 1.函数的声明与定义 通过def ...
- Python函数参数默认值的陷阱和原理深究"
本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...
随机推荐
- 基于RocketIO的高速串行协议设计与实现
随着对信息流量需求的不断增长, 传统并行接口技术成为进一步提高数据传输速率的瓶颈.过去主要用于光纤通信的串行通信技术—SERDES正在取代传统并行总线而成为高速接口技术的主流.SERDES 是串行器) ...
- iptables修改
https://fedoraproject.org/wiki/How_to_edit_iptables_rules?rd=User_talk:Rforlot Listing Rules Current ...
- less css下载及编绎工具
http://www.lesscss.net less.js下载 LESS 1.5已经放出Beta 1版本,支持source map等新特性,欢迎尝鲜.详细变更请见更新日志. 生产环境使用建议下载1. ...
- C#动态调用WCF接口(3)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...
- UI-10-plist文件及UITableView的高级应用①
课程要点: plist文件的新建与读取 给UITableView设置变化的值 单元格的删除.插入及刷新 plist文件的新建与读取 新建plist Commadn+N,iOS->Resouce- ...
- 分布式服务框架 Zookeeper(四)官方编程指南
握草,是不是加了官方两个字就可以唬人了. 使用ZooKeeper开发分布式应用 简介 这篇文档是为了那些想利用ZooKeeper的协调服务来构建分布式应用的开发人员而写滴,不相干的走一边去哈.在这儿有 ...
- ServletContext与Web应用以及Spring容器启动
一.ServletContext对象获取Demo Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象. 可以把ServletContext看作一个 ...
- Photoshop脚本之获得文件夹下所有特定后缀文件
function getAllFiles(folderName,houzhui){ folderName var regthis = new RegExp( '.+\.('+houzhui+')$', ...
- Spring MVC多项单选按钮
以下示例显示如何在使用Spring Web MVC框架的表单中使用多选按钮(RadioButton).首先使用Eclipse IDE来创建一个WEB工程,实现一个让用户可选择自己喜欢的数字的功能.并按 ...
- hdu 4587(割点的应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 思路:题目的意思很简单,就是删除任意2个节点以及关联的边,求图的最大连通分量数.我们知道删除割点 ...