"""
Python的4种传值的方式,必选传参 func(param)、默认传参func(param=value)、可选传参func(*param)、关键字传参func(**param)列举说明
"""
def test(param1,param2,param3):
print param1,param2,param3
test(1,"hello",True)
test(True,1,"hello")
"""
执行结果:
1 hello True
True 1 hello
"""
def test(param1,param2=100,param3=True):
print param1,param2,param3
test(1)
test(1,"hello",False)
test(1,param3=False,param2="hello")
"""
执行结果:
1 100 True
1 hello False
1 hello False
"""
def test(param1,param2=100,*param3):
def printP12():
print param1,param2
def printP123():
print param1,param2,param3,type(param3)
printP123() if len(param3)!=0 else printP12()
test(1,2,3,4,5,6)
li = [1,2,3,4]
tu = (1,2,3,4)
test(1,2,*tu)
"""
运行结果:
1 2 (3, 4, 5, 6) 3
1 2 (1, 2, 3, 4) 1
"""
def test(param1,param2=100,*param3,**param4):
def printP12():
print param1,param2
def printP1234():
print param1,param2,param3,param4,type(param4)
printP1234() if len(param3)!=0 and len(param4)!=0 else printP12()
test(1,10,11,a=1,b=2,c=3)
dict = {"a":1,"b":2,"c":3}
test(1,10,11,**dict)
"""
执行结果:
1 10 (11,) {'a': 1, 'c': 3, 'b': 2}
1 10 (11,) {'a': 1, 'c': 3, 'b': 2}
"""