3 ms·
namedtuples are a great feature of the language but this example could be solved just as well with a dict. Like why is `person_data` not a dict? The overall co
by whalesalad 11y ago
namedtuples are a great feature of the language but this example could be solved just as well with a dict. Like why is `person_data` not a dict?
The overall codebase used in this example is smelly.
- jdiez17 11y agonamedtuples are supposedly more space-efficient. For one, you don't have to store the keys every time.
- deleted 11y ago[deleted]
- watchdogtimer 11y agoIt's easier and cleaner to write me.age instead of me['age'], especially when you have an expression with a lot of brackets.
- nostrademons 11y agoAlso a better upgrade path to a "real" class-based solution.
- jamiesonbecker 11y agoI do like namedtuples also. I definitely agree -- dot-based access is handy! Addict is a dict subclass that offers dot-based access (pip install addict) Shortcomings are that it's not in stdlib and does have a few quirks (for example, be sure to convert a Dict to a dict with .to_dict() before json'ing). However, it's a nice and well maintained module.
- Scarblac 11y agoAt work I'm the guy who's always saying that overuse of dictionaries is a code smell. What keys does the dictionary "person_data" have? Well, originally it only contained username and email address, later we added street address (everywhere where it was used, we think). And then came preferred language, but that was only added in the functions that care about that. And so on, and so forth... Eventually all functions work with a slightly different version of a person_data dict, you're never quite sure which one a given function expects, and there is no central definition. Death by duck typing. Dicts are for algorithms, they shouldn't be the go-to tool for structs of data. That's what classes and namedtuples are for.