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/ 本篇博文已经迁移,阅读全文 ...
随机推荐
- MySQL和hive对比表结构脚本
#!/bin/bash source /etc/profile runlog='/tmp/zewei/check_schema_log' hive_database_schema=/tmp/hive_ ...
- PyCharm创建文件时自动添加头注释
进入设置 File->settings->Editor->File and Code Templates->Python Script 添加以下代码: #!/usr/bin/e ...
- linux跨主机复制文件
scp -r billing@10.200.171.111:/billdata2/user/yanhm/redis/* /newboss/billing/user/aabb 其中: 10.200.17 ...
- Eclipse: Android Device Chooser - Unknown Target
公司最近所有的项目都使用到了Android开发手机(或PDA)应用.所需要的Android开发技术并不是非常复杂,因为我们的底层方法全部使用WebServcie写好了,做Android开发的人员只需要 ...
- jqGrid 事件
jqGrid 事件 使用方法 var lastSel; jQuery("#gridid").jqGrid({ ... onSelectRow: function(id){ if(i ...
- java.lang.VerifyError: Expecting a stack map frame
https://blog.csdn.net/u013066244/article/details/78434134 对于java7而言,需要添加 -XX:-UseSplitVerifier(已实践). ...
- <转> 堆和栈的区别
一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数 ...
- 【BZOJ4724】[POI2017]Podzielno 数学+二分
[BZOJ4724][POI2017]Podzielno Description B进制数,每个数字i(i=0,1,...,B-1)有a[i]个.你要用这些数字组成一个最大的B进制数X(不能有前导零, ...
- Error: member names cannot be the same as their enclosing type
在编译的时候会遇到如下问题:member names cannot be the same as their enclosing type 原因:方法名和类名不能一样,如果一样就是一个构造函数.而构造 ...
- linux 服务器所支持的最大句柄数调高数倍(与服务器的内存数量相关)
https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(详尽版).pdf 2. [推荐]调大服务器所支持的最大文件句柄数(File Descri ...