简体   繁体   English

从类内部访问包含的静态信息的更好/正确的方法

[英]Better/correct way to access included static information from inside of a class

Here is my class: 这是我的课:

class node:
    node.maxNum = 10000
    node.maxCoord = 10000.0

    def __init__(self, num = 0, **coordsIn):

        if num > node.maxNum: raise nodeNumException
        self.num = num

        ##set default args##
        coordsDefault = {'X' : float('NaN'), 'Y' : float('NaN')}

        ##set coordinates to input##
        self.coords = coordsIn

        @property.setter
        def coords(self, **Coords):
            for Key in Coords:
                if Coords[Key] > maxCoord: raise nodeCoordException
            ##Set _coords to default, then update from coordsIn##
            self._coords = coordsDefault.update(Coords)
        @property
        def coords(self):
            return self._coords

When creating an instance, the following error is produced: 创建实例时,会产生以下错误:

Traceback (most recent call last):
    File "(stdin)", line 1, in (module)
    File "C:\Projects\CANDE\mesh.py", line 7, in __init__
    if num > node.maxNum: raise nodeNumException
NameError: name 'maxNum' is not defined

I have tried accessing the maxNum and maxCoord variables inside of my class several different ways, but I cannot seem to figure out how to avoid this error. 我尝试以几种不同的方式访问类内部的maxNummaxCoord变量,但是我似乎无法弄清楚如何避免该错误。

Is there a way to fix my code and keep the same approach? 有没有办法修复我的代码并保持相同的方法?

Also: is there a better way to do this? 另外:还有更好的方法吗? Any suggestions would be appreciated. 任何建议,将不胜感激。 This is my first major Python project. 这是我的第一个大型Python项目。

Class Variables don't require a class qualifier when declaring/defining it. 在声明/定义类变量时,它不需要类限定符。 You only need the qualifier when accessing it 访问时只需要限定符

class node:
    maxNum = 10000
    maxCoord = 10000.0

    def __init__(self, num = 0, **coordsIn):

        if num > node.maxNum: raise nodeNumException
        self.num = num
        ........

You have more problems in your code 您的代码有更多问题

  1. When defining the setter, you need a property object. 定义设置器时,需要一个属性对象。
  2. The setter should always follow the getter or else you would get a NameError. 设置器应始终跟随获取器,否则您将收到NameError。
class node:
    maxNum = 10000
    maxCoord = 10000.0

Also change the line to if Coords[Key] > node.maxCoord: raise nodeCoordException , using node to access. 同样,将行更改为if Coords[Key] > node.maxCoord: raise nodeCoordException ,使用节点进行访问。

Unrelated but you should use uppercase for class names: class Node 无关,但您应该对类名称使用大写字母: class Node

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 namedtuple 作为 args 传递信息的更好方法 - better way to pass information from namedtuple as args 从类实例中访问静态方法的正确/首选方法 - Correct/preferred way to access staticmethod from within class instance 如果可以的话,在其中一个组件内访问 class 复合材料的正确方法是什么? - What is the correct way to access a class composite inside one of its components, if that's even advisable? 有没有更好的方法来显示Django管理员中Django模型的其他信息 - Is there a better way to display additional information from the Django model in the Django admin 在python中使用XPath从表中获取信息的更好方法? - Better way of getting information from a table using XPath in python? 有没有更好的方法将信息从 for 循环写入 .txt 文件? - Is there a better way to write information from a for loop to .txt file? 在多重继承中访问超类方法的更好方法 - Better way to access super class method in multiple inheritance python中正确的方法在另一个类中创建一个类? - The correct way in python to create a class inside another class? 这是从外键模型Django获取信息的正确方法吗 - Is this the correct way to get information from forign-key model Django 从类对象列表调用类方法的更好方法 - Python - A better way to call Class Methods from List of Class Objects - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM