<legend id='HuDIl'><style id='HuDIl'><dir id='HuDIl'><q id='HuDIl'></q></dir></style></legend>

      1. <small id='HuDIl'></small><noframes id='HuDIl'>

        <tfoot id='HuDIl'></tfoot>
        <i id='HuDIl'><tr id='HuDIl'><dt id='HuDIl'><q id='HuDIl'><span id='HuDIl'><b id='HuDIl'><form id='HuDIl'><ins id='HuDIl'></ins><ul id='HuDIl'></ul><sub id='HuDIl'></sub></form><legend id='HuDIl'></legend><bdo id='HuDIl'><pre id='HuDIl'><center id='HuDIl'></center></pre></bdo></b><th id='HuDIl'></th></span></q></dt></tr></i><div id='HuDIl'><tfoot id='HuDIl'></tfoot><dl id='HuDIl'><fieldset id='HuDIl'></fieldset></dl></div>
          <bdo id='HuDIl'></bdo><ul id='HuDIl'></ul>

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

        时间:2023-09-13

        <tfoot id='hBNwf'></tfoot>
      3. <legend id='hBNwf'><style id='hBNwf'><dir id='hBNwf'><q id='hBNwf'></q></dir></style></legend>
        1. <i id='hBNwf'><tr id='hBNwf'><dt id='hBNwf'><q id='hBNwf'><span id='hBNwf'><b id='hBNwf'><form id='hBNwf'><ins id='hBNwf'></ins><ul id='hBNwf'></ul><sub id='hBNwf'></sub></form><legend id='hBNwf'></legend><bdo id='hBNwf'><pre id='hBNwf'><center id='hBNwf'></center></pre></bdo></b><th id='hBNwf'></th></span></q></dt></tr></i><div id='hBNwf'><tfoot id='hBNwf'></tfoot><dl id='hBNwf'><fieldset id='hBNwf'></fieldset></dl></div>
          • <small id='hBNwf'></small><noframes id='hBNwf'>

                • <bdo id='hBNwf'></bdo><ul id='hBNwf'></ul>
                    <tbody id='hBNwf'></tbody>
                  本文介绍了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 的类属性.

                  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 ?

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

                  上一篇:有没有办法访问包含基类的 __dict__ (或类似的东西 下一篇:数据属性和方法属性的区别

                  相关文章

                  最新文章

                • <legend id='D9c5d'><style id='D9c5d'><dir id='D9c5d'><q id='D9c5d'></q></dir></style></legend><tfoot id='D9c5d'></tfoot>

                  1. <i id='D9c5d'><tr id='D9c5d'><dt id='D9c5d'><q id='D9c5d'><span id='D9c5d'><b id='D9c5d'><form id='D9c5d'><ins id='D9c5d'></ins><ul id='D9c5d'></ul><sub id='D9c5d'></sub></form><legend id='D9c5d'></legend><bdo id='D9c5d'><pre id='D9c5d'><center id='D9c5d'></center></pre></bdo></b><th id='D9c5d'></th></span></q></dt></tr></i><div id='D9c5d'><tfoot id='D9c5d'></tfoot><dl id='D9c5d'><fieldset id='D9c5d'></fieldset></dl></div>

                    <small id='D9c5d'></small><noframes id='D9c5d'>

                      <bdo id='D9c5d'></bdo><ul id='D9c5d'></ul>