NameError:未在类本身内部定义的类的名称 - python

2022-01-13 00:00:00 python class methods attributes

问题描述

我有以下代码:

import numpy as np

class ClassProperty(property):
    def __get__(self, cls, owner):
        return self.fget.__get__(None, owner)()

def coord(cls, c):
    if cls.dimension <= 2:
        return c
    else:
        return c + [0]*(cls.dimension-2)

class Basis_NonI(object):

    @ClassProperty
    @classmethod
    def zerocoord(cls):
        return coord(cls, [0,0])   
        
    def __init__(self, dimension):
        pass

class Basis_D(Basis_NonI):
    dimension = 2
    proj_matrix = np.array([Basis_D.zerocoord, Basis_D.zerocoord])
    def __init__(self, dimension):
        super(Basis_D, self).__init__(Basis_D.dimension)

基本上我希望 dimensionproj_matrix 成为 Basis_D 的类属性.

where basically I want dimension and proj_matrixto be class attributes of Basis_D.

当我运行它时,出现以下错误:

When I run it, the following error is given:

proj_matrix = np.array([Basis_D.zerocoord, Basis_D.zerocoord])

proj_matrix = np.array([Basis_D.zerocoord, Basis_D.zerocoord])

NameError:名称Basis_D"未定义

NameError: name 'Basis_D' is not defined

--

我不明白的是,我可以在init中使用Basis_D.dimension,那么为什么在我使用它来定义时它不能识别名称Basis_Dproj_matrix?

What I don't understand is that I can use Basis_D.dimension in the init, so why does it not recongise the name Basis_D when I use it to define proj_matrix?


解决方案

class 是一个可执行语句.首次导入模块时(对于给定进程),执行 class 语句顶层的所有代码,以这种方式定义的所有名称都收集到一个字典中,然后 >class 对象是用这个dict创建的,最后绑定到类名上.IOW,此时:

class is an executable statement. When the module is first imported (for a given process), all the code at the top-level of the class statement is executed, all names defined that way are collected into a dict, then the class object is created with this dict, and finally bound to the class name. IOW, at this point:

class Basis_D(Basis_NonI):
   dimension = 2
   # HERE
   proj_matrix = np.array([Basis_D.zerocoord, Basis_D.zerocoord])

该类尚未创建,也未绑定到名称Basis_D.

the class has not yet been created nor bound to the name Basis_D.

现在调用 __init__ 时,该类已经创建并绑定到模块级名称 Basis_D,因此可以解析名称.

Now by the time __init__ is called, the class has already been created and bound to the module-level name Basis_D, so the name can be resolved.

FWIW,你不应该在你的方法中直接引用 Basis_D,而是 type(self) (甚至是 self - 如果一个名字没有解析为实例属性,而是在类中查找).

FWIW, you shouldn't directly reference Basis_D in your methods, but type(self) (or even self - if a name is not resolved as an instance attribute, it's looked up on the class).

另外,你为什么坚持使用类属性?您是否了解您的 project_matrix 数组将在 Basis_D 的所有实例之间共享?

Also, why do you insist on using class attributes ? Do you understand that your project_matrix array will be shared amongst all instances of Basis_D ?

相关文章