python闭包中的cell_contents [英] cell_contents in python closure

查看:94
本文介绍了python闭包中的cell_contents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python中用于闭包的cell_contents调用是否已更改?我知道func_closure不起作用, __ closure __ 起作用。

Has the cell_contents call for closures in python changed? I understand that func_closure does not work and __closure__ works.

func.__closure__.cell_contents
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'cell_contents'

我正在使用Python 3.4 .1。

I am using Python 3.4.1.

推荐答案


cell_contents对python中的闭包的调用是否已更改?

Has the cell_contents call for closures in python changed?

__ closure __ 是,而且一直以来都是一个元组元组(甚至在调用时也是如此) func_closure )。

__closure__ is, and always has been, a tuple of cells (even back when it was called func_closure).

每个单元格仍然具有 cell_contents 会员。但是,元组当然不是。

Each cell still has a cell_contents member. But the tuple, of course, does not.

因此,您想要的可能是其中之一:

So, what you want is probably one of these:

func.__closure__[0].cell_contents

[cell.cell_contents for cell in func.__closure__]

值得注意的是,未记录 __ closure __ 的详细信息以及CPython实现的特定于实现的功能。虽然数据模型定义了 __ closure __ 为:

It's worth noting that the details of __closure__ are undocumented and an implementation-specific feature of the CPython implementation. While the data model defines __closure__ as:


…其中没有说明这些单元格是什么,或者他们有一个名为 cell_contents 的属性。

… it doesn't say anything about what those cells are, or that they have an attribute named cell_contents.

但是在3.3+版本中,有一种记录的方式来获取此信息, inspect.getclosurevars

But in 3.3+, there's a documented way to get this information, inspect.getclosurevars:

>>> inspect.getclosurevars(func)
ClosureVars(nonlocals={'i': 1}, globals={}, builtins={}, unbound=set())

如果您想了解更多此函数返回的内容,则可以查看一下在您喜欢的解释器(可能是CPython)中如何实现它其他主要解释器都没有支持3.3)。 检查是旨在提供有用的可读源的那些模块之一,因此文档直接链接到源代码。因此,您可以了解它的工作原理,但基本上就是您所期望的;如果 __ closure __ 不是 None ,它只是创建一个dict映射 __ code__中的每个单元格名称.co_freevars 到元组中相应的 cell.cell_contents

If you want to know more than what this function returns, you may want to look at how it's implemented in your favorite interpreter (which is probably CPython, given that none of the other major interpreters support 3.3 yet). inspect is one of those modules that's designed to have helpful, readable source, so the docs link directly to the source code. So you can see how it works—but it's basically what you'd expect; if __closure__ isn't None, it just creates a dict mapping each cell name in __code__.co_freevars to the corresponding cell.cell_contents from the tuple.

如果需要更深入地讲,我不知道对闭包的内部有任何好的解释(这将是一篇不错的博客文章,我敢打赌有人写过...),但是我能在快速谷歌中找到的最好的是迈克尔·福尔德(Michael Foord) 没有什么是私有的:Python闭包(和ctypes),但CPython的函数对象代码对象可读性很强。您可能还想考虑一下< href = https://foss.heptapod.net / pypy / pypy rel = nofollow noreferrer> PyPy ,这往往要复杂一些,但这全都在Python中进行。 PEP 227 中还有一个简短的实现说明,该说明在Python中添加了闭包2.1,但说明不多。

If you want to go even deeper, I don't know of any good explanations of the internals of closures (that would make a good blog post, and I'll bet someone's written one… but the best I could find in a quick google is Michael Foord's Nothing is Private: Python Closures (and ctypes), but CPython's source for function objects and code objects is pretty readable if you know C and the Python C API. You might also want to consider looking at PyPy, which tends to be a bit more complicated, but it's all in Python. There's also a brief implementation note in PEP 227, which added closures in Python 2.1, but it doesn't explain much.

这篇关于python闭包中的cell_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆