0

In C++, the constructor of a class allows an instance to be constructed from another instance. e.g.

C::C(const C & c) {
    bala...;
    cc = c.cc;
}

In Python, what is the similar way of doing this? How may I use

c1 = C()
c2 = C(c1)

?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
frankliuao
  • 380
  • 1
  • 4
  • 14
  • There are no compile-time types in Python. Just do `c2 = c1`. – barak manos Nov 11 '16 at 11:26
  • What is `bala` supposed to be there? And your question is strange; you've written C++ code that takes an instance, and assigns its attributes to another instance, why don't you do the same in Python? – Daniel Roseman Nov 11 '16 at 11:29
  • @DanielRoseman Thanks. I actually wanted to say something more there. It can be also cc = "This is a copy " + c.cc; Something like that. The answer barak gave was a copy of the same instance also. But I really wanted something more than just a copy. – frankliuao Nov 11 '16 at 22:45
  • @barakmanos Thanks. Your solution provides a copy of c1, but in C++ there can be more in the constructor, like what I said in reply to Daniel. – frankliuao Nov 11 '16 at 22:46
  • @frankliuao,Is your problem resolved now ? Was my answer helpful ? – Shasha99 Nov 18 '16 at 16:08

1 Answers1

2

We don't mention the type while defining a variable in python. For example: if x=4, you can always set x to something else, x="shasha". No problem at all.

Note we can not overload a method in python.

Coming back to your question:

Assuming that you understand the python memory management and the difference between a reference and an actual value, You may use deepcopy feature in python:

import copy
class A(object):
    def __init__(self):
         self.a=10

x = A()

y= copy.deepcopy(x)

x.a=15

print(y.a)  # prints 10.

Note that you can always copy one object into another using = operator like y = x but it wont actually copy anything. Now both the references y and x will actually be pointing to the same instance. i.e. if you change anything using one, it will automatically be reflected into the other one:

class A(object):
    def __init__(self):
         self.a=10

x = A()

y = x

x.a=15

print(y.a)  # prints 15.

You can also create a dummy constructor as mentioned in following example:

class A:
    def __init__(self):
        self.a=10

    def dummy_constructor(self):
        temp = A()
        temp.a = (self.a + 20 - 5 )*100/10

        return temp


x=A()

y=x.dummy_constructor()

print(y.a)   #250
print(x.a)   #10
Shasha99
  • 1,746
  • 2
  • 16
  • 30
  • Thanks shasha. It's sort of what I wanted but not exactly. I was looking for something similar to C++, where if I define in the constructor like cc = c.cc (in my original post) or cc = "this is a copy " + c.cc, so you automatically know it's a copy of another instance. Something like that. If you use deepcopy, I'm afraid you have to do this by yourself, such as x=A(), y=copy.deepcopy(x), y.a="copy" + x.a. – frankliuao Nov 11 '16 at 22:51
  • You can not create a copy constructor in python as we do in c++. So you will have to go with some alternative. I have updated my explanation and added an alternative. Will that do good for you ? – Shasha99 Nov 12 '16 at 07:11