Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy constructor in python?

Is there a copy constructor in python ? If not what would I do to achieve something similar ?

The situation is that I am using a library and I have extended one of the classes there with extra functionality and I want to be able to convert the objects I get from the library to instances of my own class.

like image 412
Zitrax Avatar asked Aug 06 '09 20:08

Zitrax


People also ask

Is there any copy constructor in Python?

you can control copying in much the same way as you control pickle. deepcopy works well for doing this external to the class definition. But @Zitrax wants to do this within his class definition so that the new instance inherits the attributes (data) from an object of a different (parent) type (class).

What is copy constructor give an example?

Copy Constructor is called in the following scenarios: When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class. When the object of the same class type is passed by value as an argument.

What does copy () do in Python?

Copy List Python: copy() Method. The Python copy() method creates a copy of an existing list. The copy() method is added to the end of a list object and so it does not accept any parameters. copy() returns a new list.

How do you copy a constructor?

Copy Constructor in C++ A copy constructor is a member function that initializes an object using another object of the same class. A copy constructor has the following general function prototype: ClassName (const ClassName &old_obj);


1 Answers

I think you want the copy module

import copy  x = copy.copy(y)        # make a shallow copy of y x = copy.deepcopy(y)    # make a deep copy of y 

you can control copying in much the same way as you control pickle.

like image 137
Tom Dunham Avatar answered Oct 04 '22 01:10

Tom Dunham