1、位置参数,必须一一对应,缺一不行,多一也不行
def test(x,y,z):#位置形参 print(x) print(y) print(z) test(1,2,3)
2、关键字参数,无须一一对应,缺一不行,多一也不行
def test(x,y,z):#位置形参 print(x) print(y) print(z) test(y=1,z=2,x=3)
3、位置参数必须在关键字参数左边
4、可传入任意个参数,但要注意位置参数必须在关键字参数左边
def test(x,*args,**kwargs): print(x) print(args) print(kwargs) test(1,23456,7,8,a=9,b=10)
5、函数嵌套
name="你好" def weihou(): name = "我好" def weiweihou(): global name #拿到"你好" name="大家好" #将"你好"改为"大家好" weiweihou() print(name) print(name) weihou() print(name) #>>>你好 #>>>我好 #>>>大家好
6、风湿理论:函数即变量,向前引用,
6.1 即第5行调用时foo(),之能引用第5行之前的代码
1 def foo(): 2 print('from foo') 3 bar() 4 5 foo() 6 7 def bar(): 8 print('from bar') 9 10 #此无法运行,因为调用foo()时,bar还没有定义
6.2 运行第6行时,向前先运行第1行,运行到第3行时,再调用第4行
1 def foo(): 2 print('from foo') 3 bar() 4 def bar(): 5 print('from bar') 6 foo()