【发布时间】:2018-05-02 13:50:33
【问题描述】:
我有一个嵌套类设置,例如下面的代码 sn-p。
class test:
class child:
some_variable = None
当我尝试从另一个 .py 文件(如下所示)调用此代码时
from testing import test
t = test()
t.child.some_variable ="123"
t = test()
print(t.child.some_variable)
我得到了输出
123
我希望得到无,或者至少是一条错误消息。我试图用以下方法解决它,但同样的输出问题仍然存在。
class test:
def __init__(self):
self.child()
class child:
some_variable = None
def __init__(self):
self.some_variable = ""
调用父类时如何启动新的子类?
【问题讨论】:
-
我不确定你想在这里完成什么,但类中的类通常是不好的做法
-
@bphi 如果他正在做一些元编程,这是有道理的。虽然这里似乎不是这样。
标签: python inner-classes