5

I have a dict subclass that adds new methods and features, The main thing that I wanted to support is recursive update which works by updating every nested dict one by one unlike the dict.update method.

I'm using the copy.deepcopy function just like any other object, the problem is that it's not working when I added attribute access to this class:

__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__

Now I get this error:

KeyError: '__deepcopy__'

The copy.deepcopy function is trying to use a __deepcopy__ method on this object:

copier = getattr(x, "__deepcopy__", None)

Why after adding the __getattr__ this happened ? is there a way to fix it without implementing a __deepcopy__ method ?


I never used the __deepcopy__ method so I tried adding one, and this is what I have:

def __deepcopy__(self, memo):
    # create a new instance of this object
    new_object = type(self)()

    # iterate over the items of this object and copy each one
    for key, value in self.iteritems():
        new_object[key] = copy.deepcopy(value, memo)

    return new_object

Is this the right way to implement __deepcopy__ ?

1
  • Where's the rest of the class definition? Have you tested the implementation in the second half of your question?
    – jonrsharpe
    Sep 22, 2014 at 15:43

2 Answers 2

3

You don't need to implement __deepcopy__ here.

The problem is that your __getattr__ always calls __getitem__, which will raise a KeyError for a nonexistent attribute. But __getattr__ is expected to raise an AttributeError in this case.

The implementation of getattr catches the AttributeError, but not the KeyError, and therefore the exception is unhandled.

You should change __getattr__ to correctly raise AttributeError in this case.

0
1

In order to not repeat interjay, who seems to have ninja'd us all, this is a good way to implement what you want relatively safely:

class AttributeDict(dict):
    def __init__(self):
        self.__dict__ = self

ad = AttributeDict()
ad["a"] = 1
ad.a
#>>> 1

import copy
copy.deepcopy(ad)
#>>> {'a': 1}

As classes are just pretty dictionaries, this is actually one of the fastest ways of doing this on CPython. Because it doesn't overload __getattr__, this has the standard behaviour you'd expect error-wise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.