[英]Create instance of a class as a static variable
我想创建一个 class 的实例作为 class 的 static 变量。
例如:
class Complex:
I = Complex(0, 1)
def __init__(self, x, y):
self.x = x
self.y = y
当我这样做时,我得到NameError: name 'Complex' is not defined
。 我怎样才能做到这一点?
尝试拨打外线
class Complex:
def __init__(self, x, y):
self.x = x
self.y = y
I = Complex(0, 1)
__init__
אין לך סיבה לקרוא לאותו קלאס, כיוון שהקלאס אוטומטית מריץ את ה
你没有理由在里面调用 class,因为 class 会自动运行__init__
尝试在 class 定义之后实例化 class:
class Complex:
def __init__(self, x, y):
self.x = x
self.y = y
I = Complex(0, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.