为什么集合不能在Python中统一处理? [英] Why are collections not handled uniformly in Python?

查看:170
本文介绍了为什么集合不能在Python中统一处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,集合和列表的处理方式不同,似乎没有统一的方法来处理。例如,使用 add 方法向设置添加项目,对于 list ,使用 append 方法。我知道这背后有不同的语义,但也有常见的语义,通常一个算法与一些集合关注的共性比差异更多。

在C ++中,我可以使用一个< a href =http://www.sgi.com/tech/stl/OutputIterator.html =nofollow noreferrer> output_iterator 存储值在(几乎)任意类型的集合中,包括列表和集合。我可以编写一个算法,将这样的迭代器作为参数,并向它写入元素。然后该算法对于支持迭代器的容器(或其他设备,可以是文件)的类型是完全不可知的。如果后端容器是忽略重复的集合,那么这是调用者的决定。我的具体问题是,它已经发生了几次对我来说,我使用了例如一个列表某个任务,后来决定 set 更合适。现在我必须在我的代码中的几个地方改变 append add 。我只是想知道为什么Python没有这样的情况下的概念。

解决方案

直接的答案:这是一个设计缺陷。 >

你应该能够插入到通用插入有意义的任何容器中(例如,排除dict),使用相同的方法名。应该有一个一致的,通用的名称用于插入,例如。 添加,对应于 set.add list.append ,因此您可以添加到容器中,而无需关心您要插入的内容。



在不同类型中使用不同名称进行此操作是无意的不一致,并设置了一个较差的基本标准:库应该鼓励用户容器使用一致的API,而不是为每个基本容器提供很大不兼容的API。



在这种情况下通常不是一个实际问题:大多数时候,一个函数的结果是一个项目列表,实现它作为一个生成器。它们允许一致地处理这些(从函数的角度)以及其他形式的迭代:

  def foo ():
yield 1
yield 2
yield 3

s = set(foo())
l = list(foo b results1 = [i * 2 for i in foo()]
results2 =(i * 2 for i in foo())
for foo():
print r


Sets and lists are handled differently in Python, and there seems to be no uniform way to work with both. For example, adding an item to a set is done using the add method, and for the list it is done using the append method. I am aware that there are different semantics behind this, but there are also common semantics there, and often an algorithm that works with some collection cares more about the commonalities than the differences. The C++ STL shows that this can work, so why is there no such concept in Python?

Edit: In C++ I can use an output_iterator to store values in an (almost) arbitrary type of collection, including lists and sets. I can write an algorithm that takes such an iterator as argument and writes elements to it. The algorithm then is completely agnostic to the kind of container (or other device, may be a file) that backs the iterator. If the backing container is a set that ignores duplicates, then that is the decision of the caller. My specific problem is, that it has happened several times to me now that I used for instance a list for a certain task and later decided that set is more appropriate. Now I have to change the append to add in several places in my code. I am just wondering why Python has no concept for such cases.

解决方案

The direct answer: it's a design flaw.

You should be able to insert into any container where generic insertion makes sense (eg. excluding dict) with the same method name. There should be a consistent, generic name for insertion, eg. add, corresponding to set.add and list.append, so you can add to a container without having to care as much about what you're inserting into.

Using different names for this operation in different types is a gratuitous inconsistency, and sets a poor base standard: the library should encourage user containers to use a consistent API, rather than providing largely incompatible APIs for each basic container.

That said, it's not often a practical problem in this case: most of the time where a function's results are a list of items, implement it as a generator. They allow handling both of these consistently (from the perspective of the function), as well as other forms of iteration:

def foo():
    yield 1
    yield 2
    yield 3

s = set(foo())
l = list(foo())
results1 = [i*2 for i in foo()]
results2 = (i*2 for i in foo())
for r in foo():
    print r

这篇关于为什么集合不能在Python中统一处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆