1、定义函数 # 全局变量和局部变量名字相同 str = " aaa " # 定义了一个全局变量 def myPrint(): # global str#:使用global对一个全局变量的声明 str = " bbb " print (str) myPrint() # 结果:bbb print (str)
1、定义函数
# 全局变量和局部变量名字相同 str = "aaa" # 定义了一个全局变量 def myPrint(): #global str#:使用global对一个全局变量的声明 str = "bbb" print(str) myPrint() #结果:bbb print(str) #结果:aaa
2、缺省参数:
def myPrint(name, age = 10): print(name) print(age) myPrint("zs", 20) myPrint("zs") print("=" * 10) myPrint(name = "zs", age = 30) myPrint(name = "zs")
3、不定长参数
def myPrint(a, *params): print(a) print(params) # (10, 20, 30) myPrint("zs", 10, 20, 30)
def myPrint(a, **params): print(a) print(params) for i in params: print(i) myPrint("zs", name=‘zs‘, age = 20)
4、拆包
def myPrint(a, *args, **params): print(a) print(args) print(params) args = (1, 2, 3); params = {‘name‘:‘zs‘, ‘age‘:20}; myPrint("zs", *args, **params) 打印结果: zs (1, 2, 3) {‘name‘: ‘zs‘, ‘age‘: 20}