TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Improving your code readability with namedtuples

45 pointsby jaxonduover 9 years ago

11 comments

chronidover 9 years ago
I would like to have this as a feature in the language without having to import another &quot;magic&quot; module, something similar to case classes in Scala.<p>Let me dream :)
评论 #10483459 未加载
评论 #10483430 未加载
kaweraover 9 years ago
On going discussion on reddit about this article&#x2F;subject: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Python&#x2F;comments&#x2F;3qw7m4&#x2F;improving_your_code_readability_with_namedtuples&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Python&#x2F;comments&#x2F;3qw7m4&#x2F;improving_yo...</a>
robohamburgerover 9 years ago
When you combine namedtuples with something like simplegeneric you can write some pretty awesome functional code in python. I think of them not as just tuples with names but something akin to an immutable struct.<p>I tend to reach for namedtuples first now over classes because of this (and the fact that I don&#x27;t do as much OOP in general).
svisserover 9 years ago
One possible issue with this approach is that you may need to add methods in the future to the tuple.<p>For example, the following pattern would improve code readability as well (by making values available as attributes) but also allows you to extend it in the future when you need to add additional methods to your object:<p><pre><code> class Person(object): def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) </code></pre> Example: p = Person(name=&quot;John&quot;, age=30, weight=78) and p.age for access. When needed you can add methods to this class while still keeping the code readable.
评论 #10483514 未加载
评论 #10483377 未加载
评论 #10483522 未加载
评论 #10483476 未加载
评论 #10483395 未加载
rchover 9 years ago
I use namedtuples, and am happy to have them. What I dislike is the &#x27;rename&#x27; option and automatic conversion from whitespace or comma delimited field names.
kdazzleover 9 years ago
Not sure why you wouldn&#x27;t just use a class in this case...
评论 #10483518 未加载
评论 #10483418 未加载
whalesaladover 9 years 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?<p>The overall codebase used in this example is smelly.
评论 #10483555 未加载
评论 #10483392 未加载
评论 #10483333 未加载
acbartover 9 years ago
<p><pre><code> age, height, weight = person_data </code></pre> Silly example for such a useful tool.
S4Mover 9 years ago
Named tuples are fine, but what do they have over using classes or dictionaries?
评论 #10483755 未加载
seivanover 9 years ago
You can name your tuples in Swift as well, I just wish more people did that. It&#x27;s increasingly annoying to get a tuple and have no clue what indices are used for what.
glifchitsover 9 years ago
This is pretty great! I find myself writing classes to encapsulate small bits of information, just to have this sort of functionality.