Bryan Olson wrote: > The original question was about idioms and understanding, but > there's more to the case for list.clear. Python is "duck typed". > Consistency is the key to polymorphism: type X will work as an > actual parameter if and only if X has the required methods and > they do the expected things. > > Emptying out a collection is logically the same thing whether > that collection is a list, set, dictionary, or user-defined > SortedBag. When different types can support the same operation, > they should also support the same interface. That's what > enables polymorphism.
I agree that emptying is logically the same thing for all of these types. Beyond that, they don't seem to have a lot in common. It's quite possible to support a duck typing approach that works for all sorts of sequences, but it's fairly meaningless to use ducktyping for conceptually different types such as dicts, lists and sets. Do you really have a usecase for this? It seems to me that your argument is pretty hollow. For lists, which are mutable sequences, you add new data with .insert, .append or .extend. You replace or remove existing data using indexing l[x] or slicing l[x:y] in del or assignment statements. You can also remove data with .pop or .remove. These overlapping methods have specific use. l.remove(x) is short for del l[l.index(x)] (it's also faster, and that sometimes matter) and .pop() is there to support a stack-like behaviour. Dicts use indexing d[x] or .update to either add new data or to replace existing data. There is no distinction between these operations in dicts, since dicts are semantically so different from sequences. They have content, but no order, no specific "positions". You can delete one item at a time with del d[x], but since slices don't make sense for dicts, there is a d.clear() method to achieve this common task quickly. One could imagine that it was allowed to write "del d[]" or something like that, but then we also expect x = d[] and d[] = x to work... We probably don't want that. Sets are also semantically different, and thus use a different set of operations. They share the lack of order with dicts, but they aren't pairs, so the semantics is naturally different. They don't support indexing at all, since they neither have order nor keys. As far as I understand, the only operation which is currently used by all three collections is .pop, but that takes a different number or parameters, since these collections are conceptually different! Then we have Queues. They have a different purpose, and again, a different API, since they provide features such as blocking or non- blocking reads and writes. -- http://mail.python.org/mailman/listinfo/python-list