python之嵌套 闭包 装饰器 global、nonlocal关键字
嵌套: 在函数的内部定义函数闭包: 符合开放封闭原则:在不修改源代码与调用方式的情况下为函数添加新功能
# global 将局部变量变成全局变量
num = 100
def fn1():
global num
num = 600
return num
# nonlocal 将局部变量变成嵌套变量
def outer():
num = 888
def inner():
nonlocal num
num = 666
inner()
outer()
# 装饰器
# 在不改变源代码的形势下 添加新功能
def outer(func):
def inner(*args,**kwargs):
# pass # 新功能
res = func (*args,**kwargs) # 解压带给原功能
pass # 新功能
return res
return inner
# 使用装饰器(outer),得到新功能(inner)
@outer
def fn():
pass
res=fn()
print(res)
def foo(fn):
# 定义一个嵌套函数
def bar(a):
fn(a * (a - 1))
print("*" * 15,)
return fn(a * (a - 1))
return bar
'''
下面装饰效果相当于:foo(my_test),
my_test将会替换(装饰)成该语句的返回值;
由于foo()函数返回bar函数,因此my_test就是bar
同时,my_test 的参数 a 对应 bar 函数的参数 a
'''
@foo
def my_test(a):
print("==my_test函数==", a)
# 打印my_test函数,将看到实际上是bar函数
print(my_test,1111111)
# 下面代码看上去是调用my_test(),其实是调用bar()函数
my_test(10)
python之嵌套 闭包 装饰器 global、nonlocal关键字的更多相关文章
- python笔记3 闭包 装饰器 迭代器 生成器 内置函数 初识递归 列表推导式 字典推导式
闭包 1, 闭包是嵌套在函数中的 2, 闭包是内层函数对外层函数的变量(非全局变量)的引用(改变) 3,闭包需要将其作为一个对象返回,而且必须逐层返回,直至最外层函数的返回值 闭包例子: def a1 ...
- Python 进阶_闭包 & 装饰器
目录 目录 闭包 函数的实质和属性 闭包有什么好处 小结 装饰器 更加深入的看看装饰器的执行过程 带参数的装饰器 装饰器的叠加 小结 装饰器能解决什么问题 小结 闭包 Closure: 如果内层函数引 ...
- python高级-闭包-装饰器
闭包内容: 匿名函数:能够完成简单的功能,传递这个函数的引用,只有功能 普通函数:能够完成复杂的功能,传递这个函数的引用,只有功能 闭包:能够完成较为复杂的功能,传递这个闭包中的函数以及数据,因此传递 ...
- guxh的python笔记三:装饰器
1,函数作用域 这种情况可以顺利执行: total = 0 def run(): print(total) 这种情况会报错: total = 0 def run(): print(total) tot ...
- python笔记-4(装饰器、生成器、迭代器)
一.熟练掌握装饰器的原理 (在装饰器学习的过程中,查了看了很多资料,个人感觉走了很多的弯路,这个笔记,分享我的理解,希望能帮助到一些人.本文对装饰器的描述,侧重点是条理与逻辑思路,想通过从无到有的方式 ...
- python基础——8(装饰器)
一.nonlocal关键字 def outer(): num = 0 def inner(): # 如果想在被嵌套的函数中修改外部函数变量(名字)的值 nonlocal num # 将 L 与 E(E ...
- Python中利用函数装饰器实现备忘功能
Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下 " ...
- python函数与方法装饰器
之前用python简单写了一下斐波那契数列的递归实现(如下),发现运行速度很慢. def fib_direct(n): assert n > 0, 'invalid n' if n < 3 ...
- python基础5之装饰器
内容概要: 一.装饰器前期知识储备 1.python解释函数代码过程: python解释器从上往下顺序解释代码,碰到函数的定义代码块不会立即执行它,而是将其放在内存中,等到该函数被调用时,才执行其内部 ...
随机推荐
- nginx相关地址
http://www.nginx.cn/doc/ 中文文档 http://nginx.org/en/docs/ 英文文档 https://pan.baidu.com/s/1qWAZ ...
- iOS 开发中有关pch文件,以及pch常用的内容
一.创建pch文件.点击command+N.如下图操作 命名规则一般都是:项目名称-Prefix 第二步 OK,到这里已经把pch文件制作完毕.接下来就可以用了. pch文件一般书写的是全局都可以用到 ...
- Create Access Point on Archlinux
Create Access Point on Archlinux */--> Create Access Point on Archlinux 1 Solution Download creat ...
- [LC] 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LC] 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 2010提高组-C]关押罪犯(扩展域并查集
题:https://www.cometoj.com/problem/0073 #include<bits/stdc++.h> using namespace std; ; struct n ...
- [LC] 55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- JVM常见问题分析
JVM常见问题分析 启动,并且去查看日志 ./startup.sh && tail -f ../logs/catalina.out 常见有有以下几个问题: 1.java.lang.Ou ...
- 使用JDBC CallableStatements执行存储过程
Using JDBC CallableStatements to Execute Stored Procedures Connector / J完全实现了 java.sql.CallableState ...
- Maximum Value(CodeForces - 484B)
Maximum Value Time limit 1000 ms Memory limit 262144 kB You are given a sequence a consisting of n i ...