Python中Swithch Case语法实现
而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.使用lambdaresult = {C.Brian Beck提供了一个类 switch 来实现其他语言中switch的功能
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}[value](x)
# 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语法实现的更多相关文章
- 全面了解Python中的特殊语法:filter、map、reduce、lambda。
这篇文章主要介绍了Python中的特殊语法:filter.map.reduce.lambda介绍,本文分别对这个特殊语法给出了代码实例,需要的朋友可以参考下filter(function, seque ...
- Python中的基本语法
#Python的基本语法: #1.了解缩进 #Python中没有{}来表示一个代码块,但是Python使用缩进来完成区别代码框架 #那么在Python中一个缩进一般等于4个空格,当然你也可以使用TAB ...
- Python中的赋值语法
Python中复制语法有6种 Basic Form >>>spam = 'spam' Tuple assignment >>>spam, ham = 'spam', ...
- bash 中的case语法
CASE语法格式备忘 case $variable-name in pattern1) command1 ... .... commandN ;; pattern2|pattern3|pattern4 ...
- python中包的语法
1.什么是包? 包是一种通过".模块名"来组织python模块名称空间的方式.我们穿件的每个文件夹都可以被称为包. 但是要注意, 在python2中规定. 包内必须存在__init ...
- python中Switch/Case实现
学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch/Case功能. 方法一 通过字典实现 ...
- Python 中 PyQt5 库语法(一)
目录 PyQt5库(一) 一. 简介 1. 什么是 Qt 2. 什么是PyQt 3. 环境搭建 二. 基本结构 1. 第一个程序 2. 控件操作 3. 快速生成代码 4. 面向对象 三. 基类控件 1 ...
- python中继承的语法及案列
案例: 1 class Chinese: # 类的创建,类名首字母大写 2 eye = 'black' # 类属性的创建 3 4 def eat(self): # 实例方法创建 5 print('吃饭 ...
- python中 "is"和"=="的区别
python中"is"和"=="区别 在做leetcode的时候,在判断两个数据是否相等时使用了python中的is not,想着入乡随俗,既然入了python ...
随机推荐
- 通过VMware安装Linux操作系统
1.安装好vmvare,下载好Linux,本文采用redhat 6.5 (下载linux可参考http://www.linuxidc.com/Linux/2007-09/7399.htm) 2.选择 ...
- 自己实现的string的库函数
为了更好地理解string的各个库函数,现将几个常用的库函数用自己的方式实现如下: #include<iostream> using namespace std; #include< ...
- DirectFB学习笔记四
本篇目的,实现按钮的点击事件捕获,也就是鼠标点击,如果点击在方框范围内,则响应,在方框外,则忽略. 由于鼠标移动和点击都会产生事件,因此,我们可以在鼠标移动的时候记录坐标,在点击时比较坐标是否在方框范 ...
- LinkCode 整数排序II
http://www.lintcode.com/zh-cn/problem/sort-integers-ii/ 题目 给一组整数,按照升序排序.使用归并排序,快速排序,堆排序或者任何其他 O(n lo ...
- extern用法详解
1 基本解释 extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义. 另外,extern也可用来进行链接指定. 2 问题:ext ...
- Linux添加硬盘和挂载
1.使用fdisk -l 查看硬盘的详细信息 分析: 2.分区初始化 fdisk /dev/sdb 分析:各个参数的解析 1. 输入 m 显示所有命令列示. 2. ...
- iOS之RunLoop
RunLoop是iOS线程相关的比较重要的一个概念,无论是主线程还是子线程,都对应一个RunLoop,如果没有RunLoop,线程会马上被系统回收. 本文主要CFRunLoop的源码解析,并简单阐述一 ...
- PHP自定义函数
啊哈
- ios模拟器bug
Error: xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Libra ...
- LINQ基础(一)
LINQ(Language Integrated Query,语言集成查询),在C#语言中集成了查询语法,可以用相同的语法访问不同的数据源. LINQ提供了不同数据源的抽象层,所以可以使用相同的语法. ...