# 函数的传递
变量名指向的是变量的内存地址,那么函数名是不是也会指向函数的内存地址?
```python# -*- coding: utf-8 -*-'''@File : 10-函数的传递.py@Author : HY @Version : 1.0@Desc : None'''a=10def func(): print("hello")print(id(a))#id()函数是Python内置的函数,用于返回对象的唯一标识符#通常是对象在内存中的地址。print(type(a))print(type(func))print(func)x=funcprint(x)'''@OutPut : 2656362523216<class 'int'><class 'function'><function func at 0x0000026A7B95F0D0><function func at 0x0000026A7B95F0D0>'''``````python# -*- coding: utf-8 -*-'''@File : 10-函数的传递.py@Author : HY @Version : 1.0@Desc : None'''def func(): print("hello")x=funcx()'''@OutPut : hello'''```
经过检测证明函数名就像变量名一样指向的是函数的存储地址,所以现在可以让函数返回这个函数的函数名指向的函数地址
```python# -*- coding: utf-8 -*-'''@File : 10-函数的传递.py@Author : HY @Version : 1.0@Desc : None'''def func(): print("hello") return funcx=func()#这是第一次调用函数打印一次x()#这是根据返回的函数内存地址进行调用的函数,再一次打印# 所以这两行代码打印了两次“hello”字符串'''@OutPut : hellohello'''```
那么既然函数名可以指向调用的函数内存地址,那么相应的也可以在列表中存下来,字典{“key”:value}中也是可以的元组(value1,value2)集合{value1,value2}中都是可以存下来的
```python# -*- coding: utf-8 -*-'''@File : 10-函数的传递.py@Author : HY @Version : 1.0@Desc : None'''def func1(): print("func1")def func2(): print("func2")def func3(): print("func3")def func4(): print("func4")def func5(): print("func5")def func6(): print("func6")list_func=[func1,func2,func3,func4,func5,func6]count=range(len(list_func))for i in count : list_func[i]()'''@OutPut : func1func2func3func4func5func6'''```
# 闭包函数
闭函数:函数被封闭起来了就是闭函数
包函数:函数内部包含对外层函数作用域名字的引用
```python# -*- coding: utf-8 -*-'''@File : 10-函数的传递.py@Author : HY @Version : 1.0@Desc : None'''def func1(): z=12 def func2(): #闭函数:被封闭起来了 print(z) #包函数:调用外部函数作用域名字'''@OutPut : '''```
## 闭包函数的应用
```python# -*- coding: utf-8 -*-'''@File : 10-函数的传递.py@Author : HY @Version : 1.0@Desc : None'''def func1(a): def func2(): #闭函数:被封闭起来了 print(a) #包函数:调用外部函数作用域名字 func2()func1(12) #应用在当前函数需要一个不方便传的参数,这样在外部添加一个函数嵌套并且在此嵌套中调用闭函数'''@OutPut : 12'''```