Only tangentially related but I am really excited about PEP 764¹ (inline typed dictionaries). If it gets accepted, we can finally replace entire hierarchies of dataclasses with simple nested dictionary types and call it a day. I am currently teaching (typed) Python to a team of Windows sysadmins and it's been incredibly difficult to explain when to use a dataclass, a NamedTuple, a Pydantic model, or a dictionary. ¹)…
A Python dict that can report which keys you did not use
31–40 of 45 posts
Re: A Python dict that can report which keys you did not use
#32why not inside of __init__ self.accessed_keys = set() instead of @property def accessed_keys(self): return self._accessed_keys
With the @property you only get the “getter” and not the “setter”.
Re: A Python dict that can report which keys you did not use
#33Only tangentially related but I am really excited about PEP 764¹ (inline typed dictionaries). If it gets accepted, we can finally replace entire hierarchies of dataclasses with simple nested dictionary types and call it a day. I am currently teaching (typed) Python to a team of Windows sysadmins and it's been incredibly difficult to explain when to use a dataclass, a NamedTuple, a Pydantic model, or a dictionary. ¹)…
To be honest, that proposal sounds like it would make the problem even worse, by blurring the line between dicts and dataclasses even more.
I mean I agree w.r.t. the blurriness in general but this PEP is not going to change anything about that, in neither direction.
Re: A Python dict that can report which keys you did not use
#34Earlier quoted context omitted.
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.
[1] https://typing.python.org/en/latest/spec/overload.html
Even then, to be honest I'm a bit sceptical. Can you point at a link in the official documentation that says overriding methods of dictionaries may not work? I would have thought the link to UserDict would have mentioned that if true. What do you mean they are "runtime dependent"?
Re: A Python dict that can report which keys you did not use
#35Only tangentially related but I am really excited about PEP 764¹ (inline typed dictionaries). If it gets accepted, we can finally replace entire hierarchies of dataclasses with simple nested dictionary types and call it a day. I am currently teaching (typed) Python to a team of Windows sysadmins and it's been incredibly difficult to explain when to use a dataclass, a NamedTuple, a Pydantic model, or a dictionary. ¹)…
Re: A Python dict that can report which keys you did not use
#36Re: A Python dict that can report which keys you did not use
#37If 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
#38Earlier quoted context omitted.
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
#39Only tangentially related but I am really excited about PEP 764¹ (inline typed dictionaries). If it gets accepted, we can finally replace entire hierarchies of dataclasses with simple nested dictionary types and call it a day. I am currently teaching (typed) Python to a team of Windows sysadmins and it's been incredibly difficult to explain when to use a dataclass, a NamedTuple, a Pydantic model, or a dictionary. ¹)…
When, if ever, do you use TypedDicts?
Re: A Python dict that can report which keys you did not use
#40Earlier quoted context omitted.
To be honest, that proposal sounds like it would make the problem even worse, by blurring the line between dicts and dataclasses even more.
How does creating anonymous TypedDicts (and allowing them to be nested on the fly) blur the line "even more" when those features are not supported by dataclasses? I mean I agree w.r.t. the blurriness in general but this PEP is not going to change anything about that, in neither direction.
I get the rationale for "anonymous strict" return types, but then I think a better way would be to think up some way to accomplish that for dataclasses.