github.com/hoffmann Peter Hoffmann on Stackoverflow @peterhoffmann on twitter Peter Hoffmann on Facebook Contact me per email Subscribe to Atom Feed

Peter Hoffmann

Software Engineer
prev page next page

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

Posted on October 28, 2008
#stackoverflow #python

This my Answer to the stackoverflow question: How to copy all properties of an object to another object, in Python?:

If your class does not modify _ _ getitem _ _ or _ _ setitem _ _ for special attribute access all your attributes are stored in _ _ dict _ _ so you can do:

 nobj.__dict__ = oobj.__dict__.copy()    # just a shallow copy

If you use python properties you should look at inspect.getmembers() and filter out the ones you want to copy.