九、函数进阶(嵌套,名称空间,作用域)

一.函数的对象:
# 函数是第一类对象:函数名指向的值可以被当中参数传递

# 1.函数名可以被传递
1 name = 'jason'
2 x = name
3 print(x)
4 print(id(x))
5 
6 def func():
7     print('from func')
8 print(func)
View Code
# 2.函数名可以被当做参数传递给其他函数
def func():
    print('from func')


def index(args):
    print(args)
    args()
    print('from index')


# index(1)
index(func)
View Code
# 3.函数名可以被当做函数的返回值
def index():
    print('index')


def func():
    print('func')
    return index


res = func()
print(res)
res()
View Code

# 4.函数名可以被当做容器类型的参数

 

 1 def index():
 2     print('index')
 3 
 4 
 5 def func():
 6     print('func')
 7     return index
 8 
 9 
10 res = func()
11 print(res)
12 res()
View Code

 

# 在函数内部调用其他函数
# 可以将复杂的逻辑简单化
def my_max(x,y):
    if x > y:
        return x
    return y

def my_max4(a,b,c,d):
    res1 = my_max(a,b)
    res2 = my_max(res1,c)
    res3 = my_max(res2,d)
    return res3
print(my_max4(1,2,10,4))
View Code

 

二.函数嵌套

 1 # def outer():
 2 #     x = 1
 3 #     print('outer')
 4 #     def inner():
 5 #         print('inner')
 6 #     # print(inner)
 7 #     return inner
 8 #
 9 # res = outer()
10 # # print(res)
11 # res()
View Code
# 函数内定义函数应用场景

"""
写一个函数
该函数用户可以通过传参的不同控制函数执行不同的功能
"""
三.名称空间
1.名称空间:
# 就是放名字的地方
# 详细解释:存放的是变量名与变量值的内存地址得绑定关系的地方
# 要想访问一个变量的值 必须先去名称空间中拿到对应的名字 才能够访问变量的值

# name = 'jason'
# print(name)



x = 1

if 1 ==1 :
    y = 2

# print(y)
# while True:
#     z = 3
for i in [1,2,3]:
    print(i)
print(i)

def func():
    username = 'jason'
print(x)
func()



"""
View Code

 


名称空间的分类
(1).内置名称空间:python解释器提前给你定义好的名字(已经存放到内置名称空间中了),比如len,max,min,之前内置好的函数


(2).全局名称空间:文件级别的代码
x = 1

if 1 ==1 :
y = 2
print(y)
while True:
z = 3

x,y,z都会放到全局名称空间
if for while 无论嵌套多少层 它们内部所创建的名字都是全局名称空间的
(3)局部名称空间:函数体内创建的名字都属于局部名称空间(只要在函数体内都是)




生命周期:

内置名称空间:只要python解释器已启动立马创建 关闭python解释器的时候内置名称空间自动销毁
全局名称空间:只要你右键运行py文件会自动创建 py文件程序运行结束自动销毁
局部名称空间:函数被调用的时候自动创建 函数指向结束立即销毁(动态创建动态销毁)
  2.名称空间的查找
# len = '我是全局名称空间的len'
# def func():
# # len = '我是局部名称空间的len'
# print(len)
#
#
# # print(len) # 我现在站在的是全局的位置
# func()
# def index():
#     x = 'xxx'
#
# def index2():
#     print(x)
#     y = 666
#
# index()
# index2()

名字的查找顺序(******)
1.需要先确定你当前在哪(大前提)
1.站在全局: 全局 >>> 内置
2.站在局部: 局部 >>> 全局 >>> 内置
只能由局部》》》全局》》》内置这个方向
x=111
def outer():
    def inner():

        print('from inner',x)
        x = 66666666
    return inner
f=outer()
f()

 


# x=111
# def outer():
#     def inner():
#         print('from inner',x)
#     return inner
# f=outer()
# def func():
#     x=333
#     f()
# func()

 

"""
函数在定义阶段查找名字的顺序就已经固定了 不会因为函数的调用位置变化而改变(******)
四.作用域
# 作用域
# 全局作用域
# 全局有效: 内置名称空间 全局名称空间


# 局部作用域
# 局部有效 局部作用域


# global nonlocal
"""
1.在局部修改全局的变量
"""

# global 在局部修改全局的不可变数据类型
# x = []  # 因为列表是可变类型
x = 1  # 不可变类型
username = 'jason'
def func():
    # x.append('嘿嘿嘿')
    global x,username  # 修改全局变量 而不是创建局部名称空间
    x = 999
    username = 'egon'
func()
print(x)
print(username)

 



# nonlocal  局部修改局部
def func():
    x = 1
    def index():
        nonlocal x
        x = 2
    index()
    print(x)
func()


"""
global:局部修改全局 如果想修改多个 逗号隔开
nonlocal:局部修局部 如果想修改多个 逗号隔开

 



"""































 
posted @ 2019-07-10 15:22  凯帅  阅读(220)  评论(0编辑  收藏  举报