而python本身没有switch语句,解决方法有以下3种:
A.使用dictionary
values = {
value1: do_some_stuff1,
value2: do_some_stuff2,
...
valueN: do_some_stuffN,
}
values.get(var, do_default_stuff)()
B.使用lambda
result = {
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}[value](x)
C.Brian Beck提供了一个类 switch 来实现其他语言中switch的功能
# This class provides the functionality we want. You only need to look at
# this if you want to know how this works. It only needs to be defined
# once, no need to muck around with its internals.
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
# The following example is pretty much the exact use-case of a dictionary,
# but is included for its simplicity. Note that you can include statements
# in each suite.
v = 'ten'
for case in switch(v):
if case('one'):
print 1
break
if case('two'):
print 2
break
if case('ten'):
print 10
break
if case('eleven'):
print 11
break
if case(): # default, could also just omit condition or 'if True'
print "something else!"
# No need to break here, it'll stop anyway

  

												

Python中Swithch Case语法实现的更多相关文章

  1. 全面了解Python中的特殊语法:filter、map、reduce、lambda。

    这篇文章主要介绍了Python中的特殊语法:filter.map.reduce.lambda介绍,本文分别对这个特殊语法给出了代码实例,需要的朋友可以参考下filter(function, seque ...

  2. Python中的基本语法

    #Python的基本语法: #1.了解缩进 #Python中没有{}来表示一个代码块,但是Python使用缩进来完成区别代码框架 #那么在Python中一个缩进一般等于4个空格,当然你也可以使用TAB ...

  3. Python中的赋值语法

    Python中复制语法有6种 Basic Form >>>spam = 'spam' Tuple assignment >>>spam, ham = 'spam', ...

  4. bash 中的case语法

    CASE语法格式备忘 case $variable-name in pattern1) command1 ... .... commandN ;; pattern2|pattern3|pattern4 ...

  5. python中包的语法

    1.什么是包? 包是一种通过".模块名"来组织python模块名称空间的方式.我们穿件的每个文件夹都可以被称为包. 但是要注意, 在python2中规定. 包内必须存在__init ...

  6. python中Switch/Case实现

    学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch/Case功能. 方法一 通过字典实现 ...

  7. Python 中 PyQt5 库语法(一)

    目录 PyQt5库(一) 一. 简介 1. 什么是 Qt 2. 什么是PyQt 3. 环境搭建 二. 基本结构 1. 第一个程序 2. 控件操作 3. 快速生成代码 4. 面向对象 三. 基类控件 1 ...

  8. python中继承的语法及案列

    案例: 1 class Chinese: # 类的创建,类名首字母大写 2 eye = 'black' # 类属性的创建 3 4 def eat(self): # 实例方法创建 5 print('吃饭 ...

  9. python中 "is"和"=="的区别

    python中"is"和"=="区别 在做leetcode的时候,在判断两个数据是否相等时使用了python中的is not,想着入乡随俗,既然入了python ...

随机推荐

  1. python入门编程之基础

    Python, 是一种面向对象.解释型计算机程序设计语言.Python语法简洁清晰,特色之一是强制用空白符作为语句缩进.Python的设计哲学是"优雅"."明确" ...

  2. 深入解析Hashtable、Dictionary、SortedDictionary、SortedList

    我们先看Hashtable. MSDN的解释:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织. Hash算法是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定 ...

  3. STM32实战应用(一)——1602蓝牙时钟1液晶的显示测试

    前言 从51到STM32F4学习这么久了,总算找到点头绪了,目前学习了GPIO,中断,定时器,看门狗的基本使用,所以想试着看看能不能做个什么东西,就是想复习一下最近学习的知识.正好上学期单片机课程设计 ...

  4. IDEA第二章----配置git、tomcat(热部署)、database,让你的项目跑起来

    第一节:下载git客户端,整合idea 由于博主公司用的git版本管理,所以本系列都是基于git版本工具的,当然SVN与git配置类似.git同样支持安装版和解压版,支持各种操作系统,我这里下载的是W ...

  5. jquery ajax自定义分页组件(jquery.loehpagerv1.0)原创

    简单的两个步骤截可调用 <script src="<%=basePath%>/resources/js/jquery-1.7.1.min.js"></ ...

  6. 关于div+css布局值得注意的地方

    注意项 我们知道,如果想要两个 div(即块级元素)挨着一起排列,可以将其设置为inline-block(行内-块元素). 不过要注意两个div内的内容的对齐方式将是垂直中间对齐,所以这时候就需要使用 ...

  7. 一条sql语句引发mysql不停创建临时表的问题解决..coping to tmp table on desk

    (不懂临时表的先看 MySQL临时表 ) 首先,临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间.因此在不同的连接中可以创建同名的临时表,并且操作属于本连接的临时表.     ...

  8. oracle linux 6.5 安装 oracle 12cR2数据库(2)-DBCA建库

    援引:http://www.cnblogs.com/kerrycode/p/3386917.html  by 潇湘隐者 Oracle 12C引入了CDB与PDB的新特性,在ORACLE 12C数据库引 ...

  9. 572. Subtree of Another Tree

    Problem statement: Given two non-empty binary trees s and t, check whether tree t has exactly the sa ...

  10. IOS中常用的UIColor

    UIColor + (UIColor *)blackColor; // 0.0 white 黑色 + (UIColor *)darkGrayColor; // 0.333 white 深灰色 + (U ...