Python中为什么没有switchpython 语法结构构,有什么代替方案吗

Python中为什么没有switch语法结构,有什么代替方案吗_百度知道
Python中为什么没有switch语法结构,有什么代替方案吗
提问者采纳
弄清这个问题之后,你就用dict来实现啦 假定输入in1 执行函数fun1,输入in2 执行函数fun,你可以自己研究发现,switch实际上有一个hash表请问 C语言里switch 的本质是什么呢?如果你会反汇编的话.,系统是通过查表来实现跳转的.,转到python的话
来自团队:
其他类似问题
为您推荐:
python的相关知识
其他1条回答
用字典替代
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Python实现相仿switch.case功能 - Perl/Python当前位置:& &&&Python实现相仿switch.case功能Python实现相仿switch.case功能&&网友分享于:&&浏览:0次Python实现类似switch...case功能最近在使用Python单元测试框架构思自动化测试,在不段的重构与修改中,发现了大量的if...else之类的语法,有没有什么好的方式使Python具有C/C#/JAVA等的switch功能呢?
在不断的查找和试验中,发现了这个:/recipes/410692/,并在自己的代码中大量的应用,哈哈,下面来看下吧:
下面的类实现了我们想要的switch。
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
return False
下面是它的使用方法:
for case in switch(v):
if case('one'):
if case('two'):
if case('ten'):
if case('eleven'):
if case(): # 默认
print "something else!"
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有Java语言Switch结构_百度知道
Java语言Switch结构
用一段文字来描绘一下switch这个选择结构
而不管后面的case是否匹配:语句1.println(&quot:case 2:case3.out.case后的语句可以不用大括号,不能接受其他类型;haha&quot,利用这一特性可以让好几个case执行统一语句;case4:语句,default并不是必须的;):switch(x){case 1,short..;.switch语句的判断条件可以接受);hehe&quot,就会顺序执行后面的程序代码.例如.一旦case匹配: System关于java中switch使用的一些说明switch(表达式){case 常量表达式1;}default就是如果没有符合的case就执行它,char.println(&quot,byte.case 常量表达式2: System.out:语句2,直到遇见break.
其他类似问题
为您推荐:
其他1条回答
switch关键字的中文意思是开关、转换的意思,switch语句在条件语句中特别适合做一组变量相等的判断,在结构上比if语句要清晰很多。
switch语句的语法格式为:
switch(表达式){
功能代码1;
功能代码2;
功能代码1;
语法说明:
1、 表达式的类型只能为byte、short、char和int这4种之一。
2、 值1、值2…值n只能为常数或常量,不能为变量。
3、 功能代码部分可以写任意多句。
4、 break关键字的意思是中断,指结束switch语句,break语句为可选。
5、 case语句可以有任意多句,是标号语句。
6、 default语句可以写在switch语句中的任意位置,功能类似于if语句中的else。
执行流程:当表达式的值和对应case语句后的值相同...
java语言的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁switch后面没有条件,怎么判断的
switch后面没有条件,怎么判断的
case 10:case
6:default:
位置可以打乱。
虽然case 10:可以后面为空。但位置不可改变。即:为100分时,case 10:无输出,无结束循环语句
,那么执行他下方的case
9:。如case
6:位置对调,那么执行case
希望不光对你的问题有帮助,也对你的本节课有帮助。
写下你的评论...
写下你的评论...
写下你的评论...
写下你的评论...
写下你的评论...
Copyright (C)
All Rights Reserved | 京ICP备 号-2Python中switch Case语法实现-python-Php教程-壹聚教程网Python中switch Case语法实现
switch Case语句在python中要如何来实现呢,因为我们学习其它的编程都会有switch Case这么一个条件判断句子了,下面我们来看在Python中switch Case语法实现方法。
其他语言中,switch语句大概是这样的
switch (var)
&&& case value1: do_some_stuff1();
&&& case value2: do_some_stuff2();
&&& case valueN: do_some_stuffN();
&&& default: do_default_stuff();
而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的功能
/recipes/410692/
# 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 d for its simplicity. Note that you can include statements
# in each suite.
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
# break is used here to look as much like the real thing as possible, but
# elif is generally just as good and more concise.
# Empty suites are considered syntax errors, so intentional fall-throughs
# should contain 'pass'
for case in switch(c):
&&& if case('a'): pass # only necessary if the rest of the suite is empty
&&& if case('b'): pass
&&& if case('y'): pass
&&& if case('z'):
&&&&&&& print &c is lowercase!&
&&&&&&& break
&&& if case('A'): pass
&&& if case('Z'):
&&&&&&& print &c is uppercase!&
&&&&&&& break
&&& if case(): # default
&&&&&&& print &I dunno what c was!&
# As suggested by Pierre Quentel, you can even expand upon the
# functionality of the classic 'case' statement by matching multiple
# cases in a single shot. This greatly benefits operations such as the
# uppercase/lowercase example above:
import string
for case in switch(c):
&&& if case(*string.lowercase): # note the * for unpacking as arguments
&&&&&&& print &c is lowercase!&
&&&&&&& break
&&& if case(*string.uppercase):
&&&&&&& print &c is uppercase!&
&&&&&&& break
&&& if case('!', '?', '.'): # normal argument passing style also applies
&&&&&&& print &c is a sentence terminator!&
&&&&&&& break
&&& if case(): # default
&&&&&&& print &I dunno what c was!&
# Since Pierre's suggestion is backward-compatible with the original recipe,
# I have made the necessary modification to allow for the above usage.
---------------------------------------------------------------------------------------
获取网站类型例子:
import sys
from urllib.parse import urlparse
def getWebType(url):
&&& r = urlparse(url)
&&& urlDomain = r.netloc
&&& urlPrefix = urlDomain.split('.')[0]
&&& def www():
&&&&&&& return &www&
&&& def blog():
&&&&&&& return &blog博客&
&&& def bbs():
&&&&&&& return &BBS论坛&
&&& def tieba():
&&&&&&& return &tieba贴吧&
&&& def wiki():
&&&&&&& return &wiki维基&
&&& def baike():
&&&&&&& return &baike百科&
&&& def img():
&&&&&&& return &img图片&
&&& def ask():
&&&&&&& return &ask问答&
&&& def other():
&&&&&&& return &other&
&&& web ={
&&&&&&& &www&:www,
&&&&&&& &bbs&:bbs,
&&&&&&& &wiki&:wiki,
&&&&&&& &baike&:baike,
&&&&&&& &tieba&:tieba,
&&&&&&& &iask&:ask,
&&&&&&& &ask&:ask,
&&&&&&& &img&:img,
&&&&&&& &image&:img,
&&& return web.get(urlPrefix,other)()
url = 'http://www.111cn.net'
webtype = getWebType(url)
print(webtype)
上一页: &&&&&下一页:相关内容}

我要回帖

更多关于 java switch语法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信