[Python] Can a class contain an instance of itself as a data container?

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página