The direct answer: this is a design flaw.
You should be able to paste into any container where a generic insert makes sense (e.g. excluding a dict) with the same method name. There must be a consistent, common name for insertion, for example. add , which matches set.add and list.append , so you can add to the container without worrying about what you paste.
Using different names for this operation in different types is gratuitous inconsistency and sets a low base standard: the library should encourage user containers to use a consistent API, and not provide mostly incompatible APIs for each base container.
However, this is not often a practical problem in this case: most of the time, when the results of the function are a list of elements, implement it as a generator. They allow you to process both of these sequences (in terms of function), as well as other forms of iterations:
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
Glenn maynard
source share