#!/usr/bin/env python
import re
def hat(found):
if found: print "found a hat"
else: print "no hat"
def cat(found):
if found: print "found a cat"
else: print "no cat"
def dog(found):
if found: print "found a dog"
else: print "no dog"
st="""
Here is the target string
with a hat and a cat
no d o g
end
"""
patterns=['hat', 'cat', 'dog']
functions=[hat,cat,dog]
for pattern,case in zip(patterns,functions):
print "pattern=",pattern
case(re.search(pattern,st))
C样式的case/switch语句也“失败”,例如:
switch(c) {
case 'a':
case 'b':
case 'c': do_abc();
break;
... other cases...
}
使用元组和可调用列表,可以获得类似的行为:
st="rat kitten snake puppy bug child"
def proc1(st): print "cuddle the %s" % st
def proc2(st): print "kill the %s" % st
def proc3(st): print "pick-up the %s" % st
def proc4(st): print "wear the %s" % st
def proc5(st): print "dispose of the %s" %st
def default(st): print "%s not found" % st
dproc={ ('puppy','kitten','child'):
[proc3, proc1],
('hat','gloves'):
[proc3, proc4],
('rat','snake','bug'):
[proc2, proc3, proc5]}
for patterns,cases in dproc.iteritems():
for pattern in patterns:
if re.search(pattern,st):
for case in cases: case(pattern)
else: default(pattern)
print
import re
class Re(object):
def __init__(self):
self.last_match = None
def match(self,pattern,text):
self.last_match = re.match(pattern,text)
return self.last_match
def search(self,pattern,text):
self.last_match = re.search(pattern,text)
return self.last_match
gre = Re()
if gre.match(r'foo',text):
# do something with gre.last_match
elif gre.match(r'bar',text):
# do something with gre.last_match
else:
# do something else
你在找pyswitch(免责声明:我是作者)。使用它,您可以执行以下操作,这与您在问题中给出的示例非常接近:
在我链接的URL上有一个更全面的例子。pyswitch不仅限于打开正则表达式。在内部,pyswitch使用一个调度系统,类似于上面其他人给出的示例。我只是厌倦了每次需要这种调度系统时都要一遍又一遍地重新编写相同的代码框架,所以我编写了pyswitch。
首先考虑why there is no case statement in Python。所以让你的大脑复位,忘掉它们。
可以使用对象类、函数装饰器或函数字典来获得相同或更好的结果。
下面是一个简单的例子:
C样式的case/switch语句也“失败”,例如:
使用元组和可调用列表,可以获得类似的行为:
这得到了找到的物品的正确顺序:1)抱起孩子,拥抱孩子;2)杀死老鼠,抱起老鼠。。。用可理解的语法对C switch语句执行同样的操作是很困难的。
还有很多其他方法可以模拟C开关语句。这里有一个(用于整数)使用函数装饰符:
套用Larry Wall、Tom Christiansen、Jon Orwant在Programming Perl中关于理解Perl中的上下文的话:
快速搜索会显示前面询问的similar question以及多个工作区。我最喜欢的解决办法是米扎德
相关问题 更多 >
编程相关推荐