一 函数名的使用
python中的函数可以像变量一样,动态创建,销毁,当参数传递,作为值返回,叫第一类对象.其他语言功能有限
1.1 函数名是个特殊的变量,可以当做变量赋值

def func():
print("有志者,事竟成,破釜沉舟,百二秦关终属楚")
res = 4+3j
res = func
# res() = func()
# 把func当成变量赋值给res ,此刻res也成为了函数,调用函数需要在res后面加上()
res()

执行
[root@node10 python]# python3 test.py
有志者,事竟成,破釜沉舟,百二秦关终属楚
1.2 函数名可以作为容器类型数据的元素

def func1():
print(11)
def func2():
print(22)
def func3():
print(33)
lst = [func1,func2,func3]
print(lst)
# 循环调用列表当中的每一个函数
for i in lst:
i()

执行
[root@node10 python]# python3 test.py
[, , ]
11
22
33
1.3 函数名可以作为函数的参数

def func1(func):
# 函数的调用处
res = func()
print(res)
# 函数的定义处
def func2():
return 123
func1(func2)

执行
[root@node10 python]# python3 test.py
123
1.4 函数名可作为函数的返回值

def func1(func2):
# 返回到函数的调用处
return func2
def func2():
return 456
# 参数func2
res = func1(func2)
print(res)
print(res())

执行
[root@node10 python]# python3 test.py
456
二 全局变量和局部变量
局部变量: 定义在函数里面的变量就是局部变量
全局变量: 定义在函数外面的变量或者在函数内部用global关键字声明的变量是全局变量
局部变量的作用域: 只限定在函数内部
全局变量的作用域: 横跨整个文件
2.1 局部变量的获取和修改
def func(a):
a += 5 # 修改局部变量
print(a) # 获取局部变量
func(1)
执行
[root@node10 python]# python3 test.py
6
2.2 全局变量的获取和修改

b = 10
print(b)
# (2)在函数内部可以直接访问到全局变量
def func2():
# 在函数内部可以直接访问全局变量,但是不能直接修改全局变量
print(b) # 获取全局变量
# b+=2 # 修改全局变量
# print(b)
func2()

在函数内部可以通过global关键字修饰,进而修改全局变量
注意点:务必在函数这个代码块的开头用global关键字声明修饰

c = 12
def func3():
global c
c += 2
print(c)
func3()

执行
[root@node10 python]# python3 test.py
14
2.3 在函数内部直接声明一个全局变量

def func4():
global d
d = 90
d+=10
print(d)
func4()
print(d)

执行
[root@node10 python]# python3 test.py
100
100
global 关键字如果在函数外面有该全局变量,用在函数中是修改全局变量
global 关键字如果在函数外面没有该全局变量,用在函数中是定义全局变量
三 函数的嵌套
python中允许函数嵌套,嵌套在外层的是外函数,嵌套在里层的是内函数

def func1():
a = 10
def func2():
print(a)
func2()
func1()

执行
[root@node10 python]# python3 test.py
10
func2()
内部函数可以直接在函数外部调用么 不行
调用外部函数后,内部函数可以在函数外部调用吗 不行
内部函数可以在函数内部调用吗 可以
内部函数在函数内部调用时,是否有先后顺序 有
定义一个三层嵌套函数,最外层的是outer 第二层是inner ,第三层是smaller,调用smaller

def outer():
a = 90
def inner():
# a = 91
def smaller():
print(a)
print(id)
smaller()
inner()
outer()

执行
[root@node10 python]# python3 test.py
90
找寻变量的调用顺序采用LEGB原则(即就近原则)
B —— Builtin(Python);Python内置模块的命名空间 (内建作用域)
G —— Global(module); 函数外部所在的命名空间 (全局作用域)
E —— Enclosing function locals;外部嵌套函数的作用域(嵌套作用域)
L —— Local(function);当前函数内的作用域 (局部作用域)
依据就近原则,从下往上 从里向外 依次寻找
del删除
a01 = 10
def func():
a01 = 20
print(a01)
func()
执行
[root@node10 python]# python3 test.py
20
使用del

a01 = 10
def func():
a01 = 20
del a01
# 除非不定义,一旦定义过一次局部变量,删除后,会默认认为找不到,报错
print(a01)
func()

执行报错

四 nonlocal函数
nonlocal 专门用来修饰[局部变量] 符合LEGB原则
用来修改当前作用域上一层的[局部变量]
如果上一层没有,继续向上寻找
直到再也找不到了,直接报错
用来修改当前作用域上一层的[局部变量]

def outer():
a = 1
def inner():
nonlocal a
a += 2
print(a)
inner()
outer()

执行
[root@node10 python]# python3 test.py
3
如果上一层没有,继续向上寻找

def outer():
a = 20
def inner():
a = 10
def smaller():
nonlocal a
a+=2
print(a) #12
smaller()
print(a) #12
inner()
print(a) #20
outer()

执行
[root@node10 python]# python3 test.py
12
12
20
直到再也找不到了,直接报错

a = 80 # 全局变量 nonlocal不能修饰
def outer():
# a = 10
def inner():
def smaller():
nonlocal a
a+=2
print(a) #12
smaller()
inner()
outer()

执行
