Python 模块之间可以交换元组吗?
我有一个由很少模块(大约 4 个左右)组成的小型 Python 程序。主模块创建一个元组列表,从而表示许多记录。这些元组可通过一个返回它们的简单函数(例如,get_records()
)供其他模块使用。
但是我不确定这是否是好的设计。问题是其他模块需要知道元组中每个元素的索引。这增加了模块之间的耦合,并且对于想要使用主模块的人来说不是很透明。
我可以想到几个替代方案:
使元组元素的索引值可用作模块常量(例如,
IDX_RECORD_TITLE
、IDX_RECORD_STARTDATE
等)。这避免了像title = record[3]
这样的幻数的需要。不要使用元组,而是创建一个记录类,并返回这些类对象的列表。优点是类方法将具有不言自明的名称,例如
record.get_title()
。不要使用元组,而是使用字典。因此在这种情况下,该函数将返回一个字典列表。优点是字典键也是不言自明的(尽管使用该模块的人需要知道它们)。但这似乎是一个巨大的开销。
我发现元组是 Python 的强大优势之一(很容易传递复合数据,而无需类/对象的编码开销),因此我目前使用 (1),但仍然想知道什么是最好的方法。
I have a small Python program consisting of very few modules (about 4 or so). The main module creates a list of tuples, thereby representing a number of records. These tuples are available to the other modules through a simple function that returns them (say, get_records()
).
I am not sure if this is good design however. The problem being that the other modules need to know the indexes of each element in the tuple. This increases coupling between the modules, and isn't very transparent to someone who wants to use the main module.
I can think of a couple of alternatives:
Make the index values of the tuple elements available as module constants (e.g.,
IDX_RECORD_TITLE
,IDX_RECORD_STARTDATE
, etc.). This avoids the need of magic numbers liketitle = record[3]
.Don't use tuples, but create a record class, and return a list of these class objects. The advantage being that the class methods will have self-explaining names like
record.get_title()
.Don't use tuples, but dictionaries instead. So in this scenario, the function would return a list of dictionaries. The advantage being that the dictionary keys are also self-explanatory (though someone using the module would need to know them). But this seems like a huge overhead.
I find tuples to be one of the great strengths of Python (very easy to pass compound data around without the coding overhead of classes/objects), so I currently use (1), but still wonder what would be the best approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://docs. python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
http://docs.python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
我没有看到通过元组传递对象有任何开销或复杂性(元组也是对象)
IMO,如果元组满足您的目的,则可以轻松使用它,但正如您所看到的,约束只需切换到一个干净地表示您的数据的类,例如
您不需要添加任何 getter 或 setter 方法。
i do not see any overhead or complexity in passing objects over tuples(tuples are also objects)
IMO if tuple serves your purpose easily use it, but as you have seen the constraints just switch to a class which represent your data cleanily e.g.
You need not add any getter or setter method .
在这些情况下,我倾向于使用词典。
如果只是为了让我稍后回来使用代码时能够轻松理解事情就好了。
不知道是不是“巨大的开销”。我想这取决于您执行此操作的频率以及它的用途。我从最简单的解决方案开始,并在真正需要时进行优化。令人惊讶的是,我很少需要改变这样的事情。
In those cases, I tend to use dictionaries.
If only to have things easily understandable for myself when I come back a bit later to use the code.
I don't know if it's a "huge overhead". I guess it depends on how often you do it and what it is used for. I start off with the easiest solution and optimize when I really need to. It surprisingly seldom I need to change something like that.