How can I create an object of a child class inside parent class?
问题 I have this code in test.py: class Parent(object): def __init__(self): self.myprop1 = "some" self.myprop2 = "thing" def duplicate(self): copy = Parent() copy.myprop1 = self.myprop1 copy.myprop2 = self.myprop2 return copy And this other in test2.py: from test import Parent class Child(Parent): def __str__(self): return "{}, {}".format(self.myprop1, self.myprop2) obj1 = Child() obj2 = obj1.duplicate() obj2.myprop1 = "another" obj2.myprop2 = "stuff" # Accessing properties print("obj1, ", obj1