NameError: name of the class not defined inside th

2019-07-20 05:08发布

问题:

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?

回答1:

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 ?



回答2:

Basis_D gets created once all the statements inside it are executed and reaches the end. You can check that using globals() dictionary.

class Basis_D(Basis_NonI):
dimension = 2
print('Basis_D got created?:',bool(globals().get('Basis_D')))
#proj_matrix = np.array([Basis_D.zerocoord, Basis_D.zerocoord])
def __init__(self, dimension):
    super(Basis_D, self).__init__(Basis_D.dimension)

print('Basis_D got created?:',bool(globals().get('Basis_D')))

=================== RESTART: D:/PythonWorkspace/sometry.py ===================
Basis_D got created?: False
Basis_D got created?: True