从 Python 中的嵌套 class 引用父 class 中的对象
Refer to object in parent class from nested class in Python
在 Python (2.7) 中,可以使用嵌套的 class 声明,这有时可以方便地组织本地 classes.
但是,我无法弄清楚如何在父项 class 中引用 class,因此我可以从中导出。
一个最小的例子是这样的:
class A(object):
class B(object):
pass
class C(object):
class D(A.B): # <-- fails with "NameError: name 'A' is not defined"
pass
在给定 class 声明的嵌套结构的情况下,如何使 class D 从 class B 派生?
你不能。您不能将其引用为 A.B
,因为 A
尚未定义(您处于定义的中间),并且您不能将其引用为 B
因为根据PEP 227、class 范围内的名称不可访问:
Names in class scope are not accessible. Names are resolved in
the innermost enclosing function scope. If a class definition
occurs in a chain of nested scopes, the resolution process skips
class definitions. This rule prevents odd interactions between
class attributes and local variable access. If a name binding
operation occurs in a class definition, it creates an attribute on
the resulting class object. To access this variable in a method,
or in a function nested within a method, an attribute reference
must be used, either via self or via the class name.
An alternative would have been to allow name binding in class
scope to behave exactly like name binding in function scope. This
rule would allow class attributes to be referenced either via
attribute reference or simple name. This option was ruled out
because it would have been inconsistent with all other forms of
class and instance attribute access, which always use attribute
references. Code that used simple names would have been obscure.
也就是说,即使有可能,这种定义看起来也很晦涩,可能可以重构为更简单的东西。
编辑: 如果你真的非常希望你的 class 层次结构看起来像这样,你可以 "monkey patch" A
:
class A(object):
class B(object):
pass
class _C(object):
class D(A.B):
pass
A.C = _C
在 Python (2.7) 中,可以使用嵌套的 class 声明,这有时可以方便地组织本地 classes.
但是,我无法弄清楚如何在父项 class 中引用 class,因此我可以从中导出。
一个最小的例子是这样的:
class A(object):
class B(object):
pass
class C(object):
class D(A.B): # <-- fails with "NameError: name 'A' is not defined"
pass
在给定 class 声明的嵌套结构的情况下,如何使 class D 从 class B 派生?
你不能。您不能将其引用为 A.B
,因为 A
尚未定义(您处于定义的中间),并且您不能将其引用为 B
因为根据PEP 227、class 范围内的名称不可访问:
Names in class scope are not accessible. Names are resolved in the innermost enclosing function scope. If a class definition occurs in a chain of nested scopes, the resolution process skips class definitions. This rule prevents odd interactions between class attributes and local variable access. If a name binding operation occurs in a class definition, it creates an attribute on the resulting class object. To access this variable in a method, or in a function nested within a method, an attribute reference must be used, either via self or via the class name.
An alternative would have been to allow name binding in class scope to behave exactly like name binding in function scope. This rule would allow class attributes to be referenced either via attribute reference or simple name. This option was ruled out because it would have been inconsistent with all other forms of class and instance attribute access, which always use attribute references. Code that used simple names would have been obscure.
也就是说,即使有可能,这种定义看起来也很晦涩,可能可以重构为更简单的东西。
编辑: 如果你真的非常希望你的 class 层次结构看起来像这样,你可以 "monkey patch" A
:
class A(object):
class B(object):
pass
class _C(object):
class D(A.B):
pass
A.C = _C