我發現它們實現switch語句的功能 - >蟒蛇switch語句不工作
File = open('/file.txt','r')
String = File.readline()
String = str(String)
print String
for case in switch(String):
if case("Head"):
print "test successed"
break
if case("Small"):
print String
break
if case("Big"):
print String
break
if case():
print String
break
字符串值時,我打印出來是頭,但switch語句總是轉到最後一種情況..該函數顯然工作正常,因爲當我改變字符串與V =「頭」它工作!
任何想法出了什麼問題?
開關功能 - >
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
你應該鏈接'case'和'switch'對象/函數的實現,否則你會問一個沒人能回答的問題。 – ninjagecko
你從哪裏開始使用'switch'功能?你能指出它的來源嗎? –
我添加了函數的實現 – user573014