Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy all properties of an object to another object, in Python?

Tags:

python

Is there a library method to copy all the properties between two (already present) instances of the same class, in Python?

I mean, something like Apache Commons' PropertyUtilsBean.copyProperties()

like image 762
Joril Avatar asked Oct 28 '08 15:10

Joril


People also ask

How do you print all the properties of an object in Python?

Use Python's vars() to Print an Object's Attributes The dir() function, as shown above, prints all of the attributes of a Python object.

How do you copy an object from one object to another in Python?

To get a fully independent copy of an object you can use the copy. deepcopy() function.

How do you copy values in Python?

In Python, we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.


1 Answers

Try destination.__dict__.update(source.__dict__).

like image 176
Peter Hosey Avatar answered Sep 23 '22 19:09

Peter Hosey