Question about idioms for clearing a list

Bryan Olson fakeaddress at nowhere.org
Fri Feb 10 17:01:52 EST 2006


Magnus Lycka wrote:
> Bryan Olson wrote:
> 
>> Magnus Lycka wrote:
>>
>>> Bryan Olson wrote:
>>>
>>>>     big_union = set()
>>>>     for collection in some_iter:
>>>>         big_union.update(t)
>>>>         collection.clear()
>>>
>>>
>>> I don't understand the second one. Where did 't' come from?
>>
>>
>> Cut-and-past carelessness. Meant to update with 'collection'.
> 
> 
> If some_iter gives you dicts, the code above will throw away
> your values,  and put the set of keys in big_union. Is that what
> you meant to do?

It can be quite useful. Python only recently added the set
type. Previously, the usual way to implement sets with
efficient membership testing was to use a dict where the
keys map to some irrelevant value. The above will work for
the old technique as well as for the set type.


 > I suspect most people would find this somewhat
> surprising. For sets and "BryanLists" it will put a set of all
> the contents of those collections in big_union.

That was the description: moving everything from several
collections into a single union.


> I think this
> just verifies my previous arguments. It's rarely meaningful to
> write functions that are meaingful for all builtin collections.

That's a nonsense argument, polymorphism doesn't have to work
over every type to be useful. Functions meaningful for either
sets or lists are common; that's enough justification for
giving corresponding operations the same interface.


-- 
--Bryan



More information about the Python-list mailing list