python 中并没有switch语句,但是有一个数据类型与switch语句特别相似,它就是 dict{ key: value, ...}

下面用 dict{ key:value,..} 来简单的实现switch语句:

#!/usr/bin/env python
#coding:utf-8 from __future__ import division __version__ = 1.0 def jia(x, y, *args, **kwargs):
return x + y def jian(x, y, *args, **kwargs):
return x - y def cheng(x, y, *args, **kwargs):
return x * y def chu(x, y, *args, **kwargs):
return x / y operator = {"+":jia, "-":jian, "*":cheng, "/":chu} #核心 def fun(o, x, y):
print operator.get(o)(x, y) fun("+",3, 6)

switch 与 python字典的更多相关文章

  1. python 字典实现类似c的switch case

    #python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...

  2. Python字典和集合

    Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...

  3. python 字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  4. python字典中的元素类型

    python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...

  5. python字典copy()方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  6. python字典的常用操作方法

    Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...

  7. Python 字典(Dictionary)操作详解

    Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...

  8. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  9. Python 字典(Dictionary) setdefault()方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

随机推荐

  1. jqgrid no url reset

    如果发现 jqgrid 在运行中出现次错误,可能是以下原因 $('#@(ViewBag.tabcid + "_grid")').jqGrid('navGrid', '#@(View ...

  2. Android 四大护法之一 Service

    1.Service的概念 Service是Android 四大组件之一,是默认没有界面的运行于后台的服务程序.Service的开启方式分为启动式服务(startService)和绑定式服务(bindS ...

  3. struts基于ognl的自动类型转换需要注意的地方

    好吧,坎坷的过程我就不说了,直接上结论: 在struts2中使用基于ognl的自动类型转换时,Action中的对象属性必须同时添加get/set方法. 例如: 客户端表单: <s:form ac ...

  4. javascript 时间格式化

    添加扩展 //时间格式化扩展Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1 ...

  5. 总结-EL表达式

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ tagl ...

  6. 总结-eclipse

    1.eclipse的workspace历史记录 打开eclipse/configuration/.settings/org.eclipse.ui.ide.prefs,把RECENT_WORKSPACE ...

  7. jquery复习笔记

    Jquery基础 让一个按钮灰掉 $("button").("disabled","true"); ance desc选择器(ance代表祖 ...

  8. 在asp.net WebForms中使用路由Route

    1.新建WebForms应用程序 2.打开Global.asax文件代码如下: public class Global : System.Web.HttpApplication { protected ...

  9. HttpServletRequest获取请求参数

    private static String getRequestParameter(HttpServletRequest request, HttpServletResponse response) ...

  10. QT 调试时出现 During startup program exited with code 0xc0000135 错误

    我用的QT creator 5.70 出现上述原因是动态库加载不成功,但是QTcreator 不会提示什么动态库,具体缺乏什么动态库要用VS新建一个工程调用才可以看到,这也是QT Creator很大的 ...