How can I "override" deepcopy in Python?

Posted by Az on Stack Overflow See other posts from Stack Overflow or by Az
Published on 2010-06-14T11:01:14Z Indexed on 2010/06/14 11:02 UTC
Read the original article Hit count: 212

Filed under:
|
|
|

Hi there,

I'd like to override __deepcopy__ for a given SQLAlchemy-mapped class such that it ignores any SQLA attributes but deepcopies everything else that's part of the class.

I'm not particularly familiar with overriding any of Python's built-in objects in particular but I've got some idea as to what I want.

Let's just make a very simple class User that's mapped using SQLA.

class User(object):
    def __init__(self, user_id, name):
        self.user_id = user_id
        self.name = name

I've used dir() to see, before and after mapping, what SQLAlchemy-specific attributes there are and I've found _sa_class_manager and _sa_instance_state.

Provided those are the only ones how would I ignore that when defining __deepcopy__?
Also, are there any attributes the SQLA injects into the mapped object?


(I asked this in a previous question (as an edit a few days after I selected an answer to the main question, though) but I think I missed the train there. Apologies for that.)

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner