类可以包含其自身的实例作为数据容器吗?

发布于 2024-09-12 06:53:34 字数 330 浏览 6 评论 0原文

python 类可以包含其自身的实例吗?作为数据容器可能如下所示?

class A:
    def __init__(self, val):
        self.a = A(val)
        self.val = val

aa = A(2) 
#this will cause RuntimeError: maximum recursion depth exceeded

我的目的是使用此类作为数据容器,在它被初始化时包含一个副本,以减少深度复制操作。 它可以用作“撤消”链,以便在必要时有机会获取 init val 的值。

这样的行动可能吗?

Can a python class contain an instance of itself as a data container may look like this?

class A:
    def __init__(self, val):
        self.a = A(val)
        self.val = val

aa = A(2) 
#this will cause RuntimeError: maximum recursion depth exceeded

My purpose is using this class as a data container contain a copy inside if it when it be inited to reduce the deepcopy action.
It may used as an "undo" chain give a chance to get the init val's value when it's necessary.

Is it possible for such an action?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

负佳期 2024-09-19 06:53:34

是的,一个类可以包含它自己的一个实例,只是由于其他人描述的原因,您无法在启动时创建它。

例如,这个类会执行此操作,

class A:
    def __init__(self,value):
        self.value=value
    def setProperty(self,subvalue):
        self.innerInstance=A(subvalue)

然后您可以实例化它并设置其自身的内部副本,如下所示:

>>>OuterInstance=A(123)
>>>OuterInstance.setProperty(456)

并验证它是否可以使用:

>>>OuterInstance.innerInstance.value
456

Yes, a class can contain an instance of itself, you just can't create it on initiation for the reasons described by others.

For example this class will do it,

class A:
    def __init__(self,value):
        self.value=value
    def setProperty(self,subvalue):
        self.innerInstance=A(subvalue)

You can then instantiate it and set its inner copy of itself like this:

>>>OuterInstance=A(123)
>>>OuterInstance.setProperty(456)

And verify it worked with:

>>>OuterInstance.innerInstance.value
456
嗫嚅 2024-09-19 06:53:34

这是行不通的,原因已经给出:

  1. Python 看到 A(2) 并调用 A.__init__
  2. A.__init__ 调用 A(val)
  3. A(val) 调用 A.__init__
  4. GOTO 2

我假设您这样做是为了记录 val 的情况;也就是说,如果稍后您决定将 val 改为 3,则不会丢弃原始值 2。怎么样:

代码

class A( object ):
    @property
    def val( self ):
        return self.history[ -1 ]

    @val.setter
    def val( self, value ):
        self.history.append( value )

    def __init__( self, val ):
        self.history = [ ]
        self.val = val

解释

  • A(object):类现在应该继承自object。只是因为,基本上。
  • @property:这告诉Python每次我们请求A.val时,它都应该调用A.val()并返回结果。这是一个装饰器;查看 property 内置函数以获取更多信息。
  • @val.setter:这与上面类似,但告诉 Python 每次我们尝试分配给 A.val 时,它应该调用以下函数。它将值附加到历史列表中,而不是设置 A.val

This won't work, for the reason already given:

  1. Python sees A(2) and calls A.__init__.
  2. A.__init__ calls A(val).
  3. A(val) calls A.__init__.
  4. GOTO 2

I assume you're doing this so that you have a log of what val has been; that is, if sometime later you decide that you want val to be 3 instead, you don't throw away the original value 2. How about:

Code

class A( object ):
    @property
    def val( self ):
        return self.history[ -1 ]

    @val.setter
    def val( self, value ):
        self.history.append( value )

    def __init__( self, val ):
        self.history = [ ]
        self.val = val

Explanation

  • A( object ): classes should now inherit from object. Just because, basically.
  • @property: this tells python that every time we ask for A.val, it should call A.val() and return the result. This is a decorator; look up the property builtin function for more information.
  • @val.setter: this is similar to the above, but tells Python that every time we try to assign to A.val it should call the following function instead. Instead of setting A.val, it appends the value to the history list.
余罪 2024-09-19 06:53:34

好吧,你在 self.a 中的类也有 self.a,所以没有尽头。我不确定我明白你正在做的事情的目的,但你可以尝试这样的事情:

class A:
    def __init__(self, val, copy = True):
        if copy:
            self.a = A(val, False)
        self.val = val

Well, the class you have in self.a also has self.a, and so furher without an end. I'm not sure I get the purpose of what you are doing, but you might try something like this:

class A:
    def __init__(self, val, copy = True):
        if copy:
            self.a = A(val, False)
        self.val = val
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文