python nonlocal 什么意思_python nonlocal的理解使用

python nonlocal 什么意思_python nonlocal的理解使用nonlocal 可以将一个变量声明为非本地变量 在 python 的 lru cache 看到了使用 defdecorator func a 1defwrapper args kwargs nonlocalaa 1returnfunc returnwrappe 实例中 当 a 变量是不可变类型时 因为包装函数引用了 a 装饰器执行结束 在包装函数里改变 a 的值 需要

nonlocal 可以将一个变量声明为非本地变量, 在python的lru_cache看到了使用

def decorator(func):

a = 1

def wrapper(*args, kwargs):

nonlocal a

a += 1

return func()

return wrapper

实例中, 当a变量是不可变类型时, 因为包装函数引用了a, 装饰器执行结束, 在包装函数里改变a的值, 需要用nonlocal声明a变量. (a是自由变量了)

当a是可变类型时, 可以不用声明nonlocal a

自己再本地试一遍能理解的更加深入

lru_cache源码中的使用, 用来记录hit和miss

只贴出包装函数的部分

f _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):

# Constants shared by all lru cache instances:

sentinel = object() # unique object used to signal cache misses

make_key = _make_key # build a key from the function arguments

PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields

cache = {}

hits = misses = 0

full = False

cache_get = cache.get

cache_len = cache.__len__

lock = RLock()

root = []

root[:] = [root, root, None, None]

if maxsize == 0:

def wrapper(*args, kwds):

nonlocal misses # 要改变misses,所以用nonlocal声明

misses += 1

result = user_function(*args, kwds)

return result

elif maxsize is None:

def wrapper(*args, kwds):

# Simple caching without ordering or size limit

nonlocal hits, misses

key = make_key(args, kwds, typed)

result = cache_get(key, sentinel)

if result is not sentinel:

hits += 1

return result

misses += 1

result = user_function(*args, kwds)

cache[key] = result

return result

else:

def wrapper(*args, kwds):

# Size limited caching that tracks accesses by recency

nonlocal root, hits, misses, full

key = make_key(args, kwds, typed)

with lock:

link = cache_get(key)

if link is not None:

# Move the link to the front of the circular queue

link_prev, link_next, _key, result = link

link_prev[NEXT] = link_next

link_next[PREV] = link_prev

last = root[PREV]

last[NEXT] = root[PREV] = link

link[PREV] = last

link[NEXT] = root

hits += 1

return result

misses += 1

result = user_function(*args, kwds)

with lock:

if key in cache:

# Getting here means that this same key was added to the

# cache while the lock was released. Since the link

# update is already done, we need only return the

# computed result and update the count of misses.

pass

elif full:

# Use the old root to store the new key and result.

oldroot = root

oldroot[KEY] = key

oldroot[RESULT] = result

# Empty the oldest link and make it the new root.

# Keep a reference to the old key and old result to

# prevent their ref counts from going to zero during the

# update. That will prevent potentially arbitrary object

# clean-up code (i.e. __del__) from running while we’re

# still adjusting the links.

root = oldroot[NEXT]

oldkey = root[KEY]

oldresult = root[RESULT]

root[KEY] = root[RESULT] = None

# Now update the cache dictionary.

del cache[oldkey]

# Save the potentially reentrant cache[key] assignment

# for last, after the root and links have been put in

# a consistent state.

cache[key] = oldroot

else:

# Put result in a new link at the front of the queue.

last = root[PREV]

link = [last, root, key, result]

last[NEXT] = root[PREV] = cache[key] = link

# Use the cache_len bound method instead of the len() function

# which could potentially be wrapped in an lru_cache itself.

full = (cache_len() >= maxsize)

return result

def cache_info():

“””Report cache statistics”””

with lock:

return _CacheInfo(hits, misses, maxsize, cache_len())

def cache_clear():

“””Clear the cache and cache statistics”””

nonlocal hits, misses, full

with lock:

cache.clear()

root[:] = [root, root, None, None]

hits = misses = 0

full = False

wrapper.cache_info = cache_info

wrapper.cache_clear = cache_clear

return wrapper

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232874.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Qt中QTableView设置分页显示的三种方法[通俗易懂]

    1、已知所有数据的情况下,以MVC模式对数据进行分页处理:参考链接:http://www.cnblogs.com/caomingongli/archive/2011/11/17/2252745.html参考链接:https://blog.csdn.net/xujiali5172923/article/details/478352652、数据库很大的时候,QTableView会自动生…

    2022年4月12日
    677
  • rapidxml学习

    rapidxml学习参考:官网http://rapidxml.sourceforge.net/https://blog.csdn.net/wqvbjhc/article/details/7662931http://blog.sina.com.cn/s/blog_9b0604b40101o6fm.htmlrapidxml_print.hpp修改代码:#ifndefRAPIDXML_PRINT_HP…

    2022年7月17日
    15
  • matlab fir带通滤波,基于Matlab的FIR带通滤波器设计与实现

    matlab fir带通滤波,基于Matlab的FIR带通滤波器设计与实现mal”>3.2软件设计3.2.1数据组织方式若输入信号x(n)和滤波器的单位冲激响应h(n)在频域分别为,则其输出信号的频率响应为。根据离散傅氏变换的性质,可以得到滤波系统的差分方程:从上文Matlab的仿真过程可得到滤波器的级数N和滤波器系数h(n)。从上述可知数字滤波器实现时,主要是进行乘和加运算以及数据存取操作。在定点DSP上实现FIR滤波有两种方式:一种是用线性缓冲区实现z-1…

    2022年6月1日
    43
  • 对接第三方顺丰丰桥下单Api接口实战教程java[通俗易懂]

    对接第三方顺丰丰桥下单Api接口实战教程java[通俗易懂]对接第三方顺丰丰桥下单接口实战教程

    2025年11月20日
    3
  • 怎么设置pycharm的背景色为黑色_怎么修改pycharm背景的颜色

    怎么设置pycharm的背景色为黑色_怎么修改pycharm背景的颜色有时候我们在使用pycharm软件时,想切换pycharm软件的背景色为黑色,怎么切换pycharm软件的背景颜色为黑色?下面来分享一下方法。工具/原料pycharm软件电脑一台方法/步骤1电脑的开始菜单上找到并点击打开【pycharm】软件。2然后点击pycharm软件左上角的【File】选项,进入切换为黑色背景。3然后在弹出的菜单中点击【settings】选项。4然后点击【Appearance…

    2022年8月28日
    3
  • PMF 培训[通俗易懂]

    PMF 培训[通俗易懂]IBM有一些很有名的培训,也很有效,真的是有立竿见影的效果.过去很长时间之后,可能不记得培训的那些细节,但是培训中传达的工作方式,能给你很大的影响.进公司后3个月的时候接受了第一个,有关customerfacing的,其实是为你提供了一个商务沟通的基本模式,现在培训的内容忘记了,但是这个模式还记得:调查-承诺-执行-反馈.一个有效的商务沟通,哪怕是和客户的一个电话,都需要艺…

    2022年6月22日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号