Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类上的闭包___Python_Python 3.x_Closures_Python Internals - Fatal编程技术网

Python 类上的闭包__

Python 类上的闭包__,python,python-3.x,closures,python-internals,Python,Python 3.x,Closures,Python Internals,我最近正在逐步阅读CPython源代码,特别是在编译期间查看类的 我偶然发现了该结构的以下条目: [-- other entries --] unsigned ste_needs_class_closure : 1; /* for class scopes, true if a closure over __class__ shoul

我最近正在逐步阅读CPython源代码,特别是在编译期间查看类的

我偶然发现了该结构的以下条目:

[-- other entries --]
unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
                                         closure over __class__
                                         should be created */
[-- other entries --]
我真的看不懂它,也找不到一个python代码的例子,它实际上设置了
ste\u needs\u class\u closure==1
。在其他失败的尝试中,我尝试了以下几点:

class foo:
    y = 30
    def __init__(self):
        self.x = 50
    def foobar(self):
        def barfoo():
            print(self.x)
            print(y)
        return barfoo
但是,即使它执行,
ste\u required\u class\u closure
在执行期间的值是
0
,而不是我希望的
1

实际上改变这个值的函数是一个没有多大帮助的函数。不幸的是,它也没有任何评论赞扬它

它实际上在中使用,并带有以下注释:

/* Check if any local variables must be converted to cell variables */
我可以理解为一个概念,但找不到它发生的例子

我尝试过搜索,这个成员第一次出现的版本,但没有找到它的引用

那么,有人能解释什么是类的闭包吗?也就是说,类的局部变量什么时候转换成单元变量?理想情况下,一个在执行过程中实际显示此行为的示例将非常棒。

向我们展示了它是被添加进去的,它引用了:防止类主体干扰
\uuuuuu类
闭包

从bug报告中,该公司试图解决的问题类型的一个示例是:

在Python3中,以下代码打印
False
,因为
super()
导致从 类命名空间。取消使用
super
,它将打印
True

class X(object):
    def __init__(self):
        super().__init__()

    @property
    def __class__(self):
        return int

print (isinstance(X(), int))
(请注意,此代码使用。)

关于修补程序的功能,请参见错误报告:

该修补程序基本上会导致以下类语句:

class C(A, B, metaclass=meta):
    def f(self):
        return __class__
大致如下所示进行编译:

def _outer_C(*__args__, **__kw__):
    class _inner_C(*__args__, **__kw__):
        def f(self):
            return __class__
    __class__ = _inner_C
    return _inner_C 
C = _outer_C(A, B, metaclass=meta)
…尽管后来的一些讨论表明,
\uuuu args\uuuuu
\uuuu kw\uuuuu
的处理在最终补丁中可能已更改