美文网首页Python
Python:表驱动

Python:表驱动

作者: 锦囊喵 | 来源:发表于2020-04-15 11:27 被阅读0次
def week(i):
        switcher={
                0:'Sunday',
                1:'Monday',
                2:'Tuesday',
                3:'Wednesday',
                4:'Thursday',
                5:'Friday',
                6:'Saturday'
             }
         return switcher.get(i,"Invalid day of week")
def zero():
        return 'zero'
def one():
        return 'one'
def indirect(i):
        switcher={
                0:zero,
                1:one,
                2:lambda:'two'
                }
        func=switcher.get(i,lambda :'Invalid')
        return func()
indirect(4)
class Switcher(object):
          def indirect(self,i):
                   method_name='number_'+str(i)
                   method=getattr(self,method_name,lambda :'Invalid')
                   return method()
          def number_0(self):
                   return 'zero'
          def number_1(self):
                   return 'one'
          def number_2(self):
                   return 'two'

https://data-flair.training/blogs/python-switch-case/

相关文章

网友评论

    本文标题:Python:表驱动

    本文链接:https://www.haomeiwen.com/subject/afoqvhtx.html