Live data from Hacker News

A Python dict that can report which keys you did not use

peterbe.com

11–20 of 45 posts

Re: A Python dict that can report which keys you did not use

#11
post #4

If you're inheriting from dict to extend its behavior, there are a lot of side effects with that, and it's recommended to use https://docs.python.org/3/library/collections.html#collectio... instead.

From right above where you linked to: > The need for this class has been partially supplanted by the ability to subclass directly from dict; however, this class can be easier to work with because the underlying dictionary is accessible as an attribute. Sounds like (unless you need the dict as a separate data member) this class is a historical artefact. Unless there's some other issue you know of not mentioned in the…

dict doesn't follow the usual object protocol, and overloaded methods are runtime dependent. It's only guaranteed that non-overloaded methods are resolved least surprisingly.

Re: A Python dict that can report which keys you did not use

#13
post #4

If you're inheriting from dict to extend its behavior, there are a lot of side effects with that, and it's recommended to use https://docs.python.org/3/library/collections.html#collectio... instead.

No, that is not the recommendation. People routinely and reliably inherit from dict.

The UserDict class is mostly defunct and is only still in the standard library because there were a few existing uses that were hard to replace (such as avoiding base class conflicts in multiple inheritance).

Re: A Python dict that can report which keys you did not use

#16

Earlier quoted context omitted.

I didn't know about the setdefault method, and wouldn't have guessed it lets you read a value. Interesting, thanks. Another way to get data out would be to use the new | operator (i.e. x = {} | y essentially copies dictionary x to y) or the update method or ** unpacking operator (e.g. x = {**y}). But maybe those come under the umbrella of iterating as you mentioned.

setdefault was a go to method before defaultdict was added to the collections module in Python 2.5, which replaced the biggest use case.

It's been some time since I last benchmarked defaultdict but last time I did (circa 3.6 and less?), it was considerably slower than judicious use of setdefault.

Re: A Python dict that can report which keys you did not use

#17
post #4

If you're inheriting from dict to extend its behavior, there are a lot of side effects with that, and it's recommended to use https://docs.python.org/3/library/collections.html#collectio... instead.

No, that is not the recommendation. People routinely and reliably inherit from dict. The UserDict class is mostly defunct and is only still in the standard library because there were a few existing uses that were hard to replace (such as avoiding base class conflicts in multiple inheritance).

UserDict is not formally deprecated but it will be someday, so code that relies on it is not future-proof.

Re: A Python dict that can report which keys you did not use

#19
post #14

I actually wrote something similar in nodejs for a data import system. Was very handy.

Interesting! Can you elaborate a little bit more on your implementation?

Mine was a bit more specific. I had a JSON object of data exported per account I was importing, and then a complex mapping (also JSON) of where to put each piece of data.

Therefore, I really wanted to know that I was actually pulling in all of the data I needed, so I tracked what was seen vs not seen, and compared against what was attempted to see.

In the end it was basically a wrapper around the JSON object itself, that allowed lookup of data via a string in "dot notation" (so you could do "keyA.key2" to get the same thing you would have directly in JSON. Then, it would either return a simple value (if there was one), or another instance of the wrapper if the result was itself an object (or an array or wrapped objects). All instances would share the "seen" list.

It's unfortunately locked behind NDA/copyright stuff, but the implementation was only 67 lines.

Re: A Python dict that can report which keys you did not use

#20
I have a similar use case and this idea also occurred to me.

However: the dict in this case would also include dataclasses, and I’d be interested in finding what exact attributes within those dataclasses were accessed, and also be able to mark all attributes in those dataclasses as accessed if the parent dataclasses is accessed, and with those dataclasses, being config objects, being able to do the same to its own children, so that the topmost dictionary has a tree of all accessed keys.

I couldn’t figure out how to do that, but welcome to ideas.

Post reply on HN