5 ms·
Just Use Dictionaries (Python)
- chrisjj 3y ago> A Python dictionary has a simple & well-known API. Actually it doesn't.
- david-j-vujic 3y agoI was referring to the simplistic and well-known way of accessing data from a dictionary, such getting the value for a specific key or even iterating the keys & values of a dictionary. There's also quite simple to merge several dictionaries, or even pick/omit to only work with the data that is actually needed in the actual context. What's your thoughts about that?
- chrisjj 3y agoThanks for asking. It is simple. It is not simplistic. It is not an API.
- david-j-vujic 3y agoWhat is it then?
- chrisjj 3y agoAs you said. A way of accessing data from a dictionary. Or more generally, a language feature.
- david-j-vujic 3y agoWould you agree that it is well-known and simple?
- chrisjj 3y agoYes!
- jstx1 3y agoI like the simplicity but there are some social problems with this. Let's say that I work on a team which has codebases with classes/dataclasses and I add a new codebase that doesn't use them, or one that doesn't _underscore just about everything - the first thing that will come up in the code review is questions about why I'm doing things differently, and I wouldn't have a good reason apart from the fact that I find it more aesthetically pleasing.
- david-j-vujic 3y agoI agree on that it is a good idea to use patterns that the teams have agreed on. Would you do the same if you were about to make changes in a code base with only dictionaries and lists in it? :)
- sametmax 3y agoI used to be a strong proponent of this, but I tend to use dataclasses more and more with time. Still use a lot of dicts, lists, and sets, but mostly as collections, rarely to hold one single element data. It came naturally, not out of some design, but I guess having better and better typing support everywhere makes it more and more convenient.
- david-j-vujic 3y agoSounds like a good balance between the approaches! I usually would prefer having everything behind the endpoint (such as Pydantic schemas & FastAPI) as simple dicts and lists.
- JoeyBananas 3y agoExcept when you need Pandas dataframes or Apache Arrows
- david-j-vujic 3y agoI haven’t used those tools, but I guess you would use the data types already defined in them and what is returned from the functions in there? If you were about to pass on the result from a calculation to somewhere else, a dictionary or list would probably be a good idea. You probably wouldn’t want the entire system be aware of a Pandas specific data type.