Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

AttrDict is not a dict #18

Closed
rbu opened this issue Dec 4, 2014 · 1 comment
Closed

AttrDict is not a dict #18

rbu opened this issue Dec 4, 2014 · 1 comment

Comments

@rbu
Copy link

rbu commented Dec 4, 2014

The project description ready:

A dictionary that allows attribute-style access.

However, I discovered:

> from attrdict import AttrDict
> a = AttrDict()
> isinstance(a, dict)
False

Is there a reason AttrDict is not extending dict? It also makes it impossible to use AttrDict as a "drop in" replacement in existing APIs.
Consider an API that used to return (or receive) a dict and could be switched to an AttrDict.

@bcj
Copy link
Owner

bcj commented Dec 5, 2014

AttrDict subclasses collections.MutableMapping which is what dict, collections.OrderedDict, collections.defaultdict, and collections.Counter all extend (incidentally none of those others subclass dict either). As far as I know, this is the recommended solution (perhaps a holdover from the time when it was impossible to subclass dict).

In most cases someone doing an insinstance(_, dict), people should probably be using insinstance(_, collections.Mapping) or insinstance(_, collections.MutableMapping). In the worst case scenario, you can always just cast it to a dict.

I've thus far avoided subclassing dict because I'm not convinced the remaining unimplemented methods are worth reducing the key-space. Which means I should probably reword it as "Can be used most places a dict can".

If you are in a situation in which you need AttrDict to subclass dict (or it would otherwise make things much easier), I'd very much appreciate it if you could comment on this issue (#21). The change would be somewhat bigger and I'm trying to gauge interest.

I'm going to close this issue as it's resolution/non-resolution will be the same as the linked issue.

@bcj bcj closed this as completed Dec 5, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants