A
A
allex9972020-11-09 18:02:51
Python
allex997, 2020-11-09 18:02:51

How can I prevent a child object from referencing a parent object?

My problem is that the created object inside the class refers to the object that created it. How to fix ?

import copy as cp
class a():
      lis = []
        
      def create_a(self):
          obj = cp.copy(a())
          print(obj.lis is self.lis) # True
     
    
 b = a()
        
 b.create_a()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-11-09
@allex997

This is lis = []done not for the class, but inside __init__Well, figure out how the class attribute differs from the object attribute (object/instance).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question