TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

A better namedtuple for Python

2 点作者 tjomk超过 10 年前

1 comment

dalke超过 10 年前
Interesting article. The author asks &quot;So, is the namedfields decorator better than the namedtuple factory function?&quot;<p>I have another observation. I like namedtuple as a quick-and-dirty class constructor when there are many parameters, as otherwise all of the __init__(self, x, ..) : self.x = x statements get tedious. (Some people have an editor macro for this, but I don&#x27;t.)<p>As soon as that object is part of the external API, I have to rewrite it as a regular class, because namedtuple has getitem lookup, and I don&#x27;t want obj[2] to be part of the public API that I need to maintain forever.<p>There are times when attribute and index lookup is appropriate. RGB and XYZ values, and of course things like os.stat(), where there was a tuple but where attribute use is better.<p>I therefore like the idea of<p><pre><code> @namedfields(&quot;spam eggs ham&quot;) class Viking: pass </code></pre> where the [0], [1], and [2] aren&#x27;t built-in.