switch(封装)

#迭代器
class
switch(object): def __init__(self,value): self.value=value self.fall=False
#迭代器方法 def __iter__(self):
yield self.match raise StopIteration def match(self,*args): if self.fall or not args: return True elif self.value in args: self.fall=True return True else: return False

调用

    v='ten'
    for case in switch(v):
        if case('one'):
            print 1
            break
        if case('ten'):
            print 10
            break

 

posted @ 2015-02-02 15:33  有态度的80后  阅读(699)  评论(0编辑  收藏  举报