函数语法组合结构:
并列函数类型:
def func1(x,y=3):
sum = x+y
return sum
def func2(x,y):
s = x*y
return s
def func3(x,y):
ss = x+y
return ss
if __name__ == '__main__':
a = func1(1,y=2) #a用来接收func1的返回值
b = func2(a,2) # 将func1的返回值a作为参数传入func2中
c = func3(a,b) # 将func1的返回值a和func2的返回值b作为参数传入func3中
包含函数类型&函数作用域:
def func(a,b,c):
def test1(x):
y = 2*x + 1
return y
line = test(a)
print(line)
def test2(x):
y = 2*x + 1
return y
line = test(b)
print(line)
def test3(x):
y = 2*x + 1
return y
line = test(c)
print(line)
func(1,2,3)
print(test(1)) #这里将会显示未定义,超出了函数的作用域
闭包:
函数是一个对象,所以可以作为某个函数的返回结果
def func():
def test1(x):
return 2*x + 1
return test1
my_func = func()
print(my_func(3))