Python内部类变量namesp

2024-10-04 01:35:36 发布

您现在位置:Python中文网/ 问答频道 /正文

class myouterclass(object):
                    def __init__(self,ID):
                              self.myouterclassID=myouterclassID
                              self.myinnerclass=self.MYINNERCLASS()
                    class MYINNERCLASS:
                              def __init__(self):
                                      self.myinnerclassID=myinnerclassID

我正在尝试创建一个内部类,并创建一些包含ID的变量。为了简单起见,我还想使用ID来定义外部类的不同实例

我对自己做错了什么缺乏了解

我试图实现的是使用同一名称变量“ID”来定义外部类的不同实例的标识,并且使用同一名称变量“ID”来跟踪内部类对象“myinnerclass”的不同实例。我计划创建多个innerclass实例,我需要有不同的ID来跟踪它们

谢谢


Tags: 实例self名称id定义objectinitdef
1条回答
网友
1楼 · 发布于 2024-10-04 01:35:36

也许是你想要的

class myouterclass(object):
    def __init__(self, the_id):
        self.myouterclassID = the_id
        self.myinnerclass = self.MYINNERCLASS(the_id)

    class MYINNERCLASS:
        def __init__(self, inner_id):
            self.myinnerclassID = inner_id
    def __init__(self, inner_id):
        self.myinnerclassID = inner_id

相关问题 更多 >