0

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.myprop1, ", ", obj1.myprop2)
print("obj2, ", obj2.myprop1, ", ", obj2.myprop2)
# Using Child method
print("obj1,", str(obj1))
print("obj2,", str(obj2))

Running test2.py, the output is:

obj1, some, thing
obj2, another, stuff
obj1, some, thing
obj2, <test.Parent object at 0x7fc1558e46d8>

I wonder if I can create an instance of Child inside Parent, but because there could be more child, I want to know the class of self and create an instance of that one class, copy the attributes and then return the object copy.

The goal for this code is to output this:

obj1, some, thing
obj2, another, stuff
obj1, some, thing
obj2, another, stuff

This means that obj2 is a Child object instead of a Parent object.

Hope this is clear, thanks!

EDIT: I don't want to use copy.copy() or copy.deepcopy(). If you want to get only a copy and implement a simpler solution, check Moberg comment to see another related question that uses those functions. But, this question is intended to get another way of doing that and also know how to get the Class from an object and get another instance of that same Class. This particular case, is showing a relationship of parent-child between classes, that I added to show the whole context of my doubt.

gorandp
  • 500
  • 5
  • 11
  • As an aside, why doesn't `Parent.__init__` take parameters? – chepner Apr 06 '20 at 20:02
  • Does this answer your question? [How can I create a copy of an object in Python?](https://stackoverflow.com/questions/4794244/how-can-i-create-a-copy-of-an-object-in-python) – Moberg Apr 06 '20 at 20:04
  • @chepner thanks for asking, do you mean putting the string literals as arguments to the child constructor so the Parent.__init__ takes and assign that? If that's it, this is because I want to know how to create an instance of a child class inside the parent class, and I created this simple code to show this particular problem and context the minimal I can. – gorandp Apr 06 '20 at 20:27
  • @Moberg thanks for your comment and that's a good alternative. Although, it replaces the ```duplicate()``` method, and I wanted to know how this could be done without using ```copy()``` or ```deepcopy()```, because both didn't work for me in the code that I am currently working on. So I will edit the question to add that I don't want to use copy or deepcopy. Thanks! :) – gorandp Apr 06 '20 at 20:34

4 Answers4

1

Just don't hard-code the class, use type to retrieve the class of the instance, something like:

class Parent(object):
    def __init__(self):
        self.myprop1 = "some"
        self.myprop2 = "thing"

    def duplicate(self):
        cls = type(self)
        copy = cls()
        copy.myprop1 = self.myprop1
        copy.myprop2 = self.myprop2
        return copy
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
0

To create the instance in duplicate you could use:

def duplicate(self):
    copy = type(self)()
    ...

But a better solution would be to use copy.copy

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
0

Yes, use type(self) to get the type of the object. Also consider implementing the duplication with the dunder method __copy__ to support the builtin copy().

def __copy__(self):
    return type(self)()
Moberg
  • 5,253
  • 4
  • 38
  • 54
0

A class method might be better, as it gives you control over the type of object being created.

class Parent(object):
    def __init__(self):
        self.myprop1 = "some"
        self.myprop2 = "thing"

    @classmethod
    def from_object(cls: 'T', obj: Parent) -> 'T':
        copy = cls()
        copy.myprop1 = obj.myprop1
        copy.myprop2 = obj.myprop2
        return copy


class Child(Parent):
   def __str__(self):
        return "{}, {}".format(self.myprop1, self.myprop2)


obj1 = Child()
obj2 = Child.from_object(obj1)  # A new Child
obj3 = Parent.from_object(obj1)  # A new Parent
chepner
  • 497,756
  • 71
  • 530
  • 681