NameError:未在类本身内部定义的类的名称-python [英] NameError: name of the class not defined inside the class itself - python

查看:109
本文介绍了NameError:未在类本身内部定义的类的名称-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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的类属性.

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

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

NameError:名称'Basis_D'未定义

-

我不了解的是,我可以在init中使用Basis_D.dimension,所以为什么当我使用它定义proj_matrix时却不识别名称Basis_D?

解决方案

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

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

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

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

FWIW,您不应在方法中直接引用Basis_D,而应直接引用type(self)(甚至是self-如果未将名称解析为实例属性,则会在类上查找它).

此外,为什么还要坚持使用类属性?您了解project_matrix数组将在Basis_D的所有实例之间共享吗?

I have the following code:

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)

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])

NameError: name 'Basis_D' is not defined

--

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 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])

the class has not yet been created nor bound to the name 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, 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).

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 ?

这篇关于NameError:未在类本身内部定义的类的名称-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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