Implicit conversion to boolean in if and while statements

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Feb 10 06:37:46 EST 2013


Rick Johnson wrote:

> IMO "Set Types" should only exists as a concequence of "freezing" an
> array,

Sets are not frozen lists.

> and should have NO literal syntax avaiable. 

Unfortunately, Python has a minor design flaw. One of the most common
use-cases for sets is for membership testing of literal sets:

def example(arg):
    if arg in {'spam', 'ham', 'eggs', 'cheese'}:
        ...

Unfortunately, set literals create *mutable* sets, not frozensets. That
means that the compiler wastes time and memory creating am over-allocated
mutable set object. If set literals created immutable frozensets, there
would be some nice opportunities for the compiler to optimize this
use-case.

So, in Python 4000, my vote is for set literals { } to create frozensets,
and if you want a mutable set, you have to use the set() type directly.


-- 
Steven




More information about the Python-list mailing list