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 函数名作为字典值的更多相关文章

  1. python函数2(返回值、传递列表...)

    python函数2(返回值.传递列表...) 1.返回值 1.1.返回简单的值 #返回简单值 def get_formatted_name(first_name,last_name): "& ...

  2. 9 - Python函数定义-位置参数-返回值

    目录 1 函数介绍 1.1 为什么要使用函数 1.2 Python中的函数 2 函数的基本使用 3 函数的参数 3.1 参数的默认值 3.2 可变参数 3.2.1 可变位置传参 3.2.2 可变关键字 ...

  3. python函数基础-参数-返回值-注释-01

    什么是函数 函数就是有特定功能的工具 # python中有内置函数(python解释器预先封装好的)与自定义函数(用户自定义封装的)之分 为什么要用函数 # 可以减少代码冗余,增加代码复用性 # 使代 ...

  4. Python函数变量和返回值

    Python函数的全局变量和局部变量 1.不同的编程语言,程序可以分为函数和过程两大类,函数具有具体返回值,而过程则不具有具体的返回值,python只具有函数,因为对于它的一般函数,其返回值为所具体返 ...

  5. python函数1_参数,返回值和嵌套

    函数 将重复的代码,封装到函数,只要使用直接找函数 函数可以增强代码的模块化和提高代码的重复利用率 函数的定义和调用 格式 def 函数名([参数,参数...]): 函数体 定义函数 import r ...

  6. Python函数(一)-return返回值

    定义一个函数可以在最后加上return返回值,方便查看函数是否运行完成和返回函数的值 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR&qu ...

  7. python 函数返回多个值

    参考文献:http://blog.csdn.net/facevoid/article/details/5369146

  8. python函数,定义,参数,返回值

    python中可以将某些具备一定功能的代码写成一个函数,通过函数可以在一定程度上减少代码的冗余,节约书写代码的时间.因为有一些代码实现的功能我们可能会在很多地方用到. 1.函数的声明与定义 通过def ...

  9. Python函数参数默认值的陷阱和原理深究"

    本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

随机推荐

  1. protobuf 在win10系统如何编译jar包

    最近在搞java服务器项目,前段要求用protobuf进行数据传输,以前没搞过,查了很多资料,走了一些弯路! 先把一些需要下载的链接放上来: protobuf下载地址:https://github.c ...

  2. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  3. 2. Trailing Zeros【easy】

    2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...

  4. 大数据(3) - 高可用 HDFS HA

    HDFS HA高可用 1 HA概述 1)所谓HA(high available),即高可用(7*24小时不中断服务). 2)实现高可用最关键的策略是消除单点故障.HA严格来说应该分成各个组件的HA机制 ...

  5. 配置LANMP环境(5)-- 安装NGINX与配置

    安装nginx yum install nginx 若提示找不到nginx,则在软件源中添加nginx的软件源文件: vim /etc/yum.repos.d/nginx.repo 添加如下内容: [ ...

  6. Erlang 和 Elixir的差异

    原文: http://elixir-lang.org/crash-course.html 函数调用 Elixir允许你调用函数的时候省略括号, Erlang不行. Erlang Elixir some ...

  7. Datagrid方法扩展 - tooltip

    最新版本的jQuery Easyui新出来了一个组件叫tooltip,就是提示框.以前的版本没有这个组件的时候就有很多在问,datagrid的单元格,我要加鼠标提示要怎么做.原来我告诉他们的方法都是用 ...

  8. Fragment嵌套Fragment实现的Tab导航

    要实现的效果就是底部导航,具体到每一个Fragment又在上部设置一个导航条,分成两个Fragment实现.效果图是: 首先给出activity的layout: <android.support ...

  9. 集合映射中的映射包(使用xml文件)

    如果持久类有List对象,我们可以通过列表或者bag元素在映射文件中映射. 这个包(bag)就像List一样,但它不需要索引元素. 在这里,我们使用论坛的场景: 论坛中一个问题有多个答案. 我们来看看 ...

  10. SourceInsight 不断无响应的有关问题

    SourceInsight 不断无响应的问题 sourceinsight3使用过程中,如果是大工程,总是出现抽筋的现象,CPU使用.内存占用都很高,阅读android源码不断出现无响应.后发现主要是因 ...