[英]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. 我尝试以几种不同的方式访问类内部的
maxNum
和maxCoord
变量,但是我似乎无法弄清楚如何避免该错误。
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 您的代码有更多问题
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.