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…
A Python dict that can report which keys you did not use
11–20 of 45 posts
Re: A Python dict that can report which keys you did not use
#12 self.accessed_keys = set()
instead of @property
def accessed_keys(self):
return self._accessed_keysRe: A Python dict that can report which keys you did not use
#13If 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.
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
#14Re: A Python dict that can report which keys you did not use
#15I actually wrote something similar in nodejs for a data import system. Was very handy.
Re: A Python dict that can report which keys you did not use
#16Earlier 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.
Re: A Python dict that can report which keys you did not use
#17If 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
#18Re: A Python dict that can report which keys you did not use
#19I 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?
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
#20However: 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.