I have a class hierarchy representing linear algebra expression (as expression trees), something like this (it’s actually more complicated than that, but that should be enough to give you an idea).
- Expression
- Operator
- Times
- Plus
- Symbol
- Matrix
- Scalar
- Operator
I’m manipulating those expression in many different ways, so I have to copy those expression a lot because I want to explore different ways of manipulating them (I really don’t see any way around copying them). Not surprisingly, copy.deepcopy() is slow.
Not all class attributes need do be deepcopied, and those expression trees will always be trees (if not, something is fundamentally wrong), so I can probably save some time by simplifying the memoization (or don’t use it at all).
I want to make copying faster by implementing a custom copy function. This could either mean implementing __deepcopy__(), writing an entirely new function or using something like get_state and set_state, I really don’t care. At the moment, I don’t care about pickling.
I’m using Python 3.
Since I have this class hierarchy where new attributes are introduced at different levels, I would prefer not to start from scratch on each level. Instead, it would be desirable to reuse some functionality of the superclass similar to how in __init__, one usually calls __init__ of the superclass. However, I would prefer not to call __init__ because that sometimes does some extra stuff that is not necessary when copying.
How do I do that in the fastest and most pythonic way? I was not able to find any reasonable guidelines regarding how to implement this copy functionality under those circumstances. I looked at the implementation of deepcopy both in Python 2 and 3. In Python 2, a bunch of different methods are used (get_state/set_state, using __dict__,…), in Python 3, I’m not able to find the corresponding function at all.