[原]python中实现switch

# -*- coding: utf-8 -*- 
#########################################
#    python switch
#########################################
def case1(testport,port):
         print "testpor + port =",(testport+port)
         
def case2(port2,testport):
     for port in testport:
         print "port*2 =",port2*2

  
def case3(port3,testport):
    for port in testport:
         print "port*2 =",port3*3
    return port3
    
                 
sw = {
    'a': lambda testport,port: case1(testport,port),                    
    'b': lambda port,testport: case2(port,testport),
    'c': lambda port,testport: case3(port,testport)
}

print sw['a'](3,3)
print sw['b'](3,[1,5])
print sw['c'](3,[1,6,7])

注意:case1和case2带有默认返回值,默认返回值为None,case3的返回值为port3

在PYTHON中,函数没有定义返回的数据类型。Python 不需要指定返回值的数据类型;甚至不需要指定是否有返回值。实际上,每个 Python 函数都返回一个值;如果函数执行过 return 语句,它将返回指定的值,否则将返回 None (Python 的空值)

作者:liuheng123456 发表于2013-1-7 17:48:05 原文链接
阅读:227 评论:0 查看评论
posted @ 2013-01-07 17:48  狼秦  阅读(334)  评论(0编辑  收藏  举报