[Python-Dev] PEP 3103: A Switch/Case Statement

K.S.Sreeram sreeram at tachyontech.net
Tue Jun 27 08:33:39 CEST 2006

Guido van Rossum wrote:
> I think we need a PEP for const/static/only/cached/precomputed or
> whatever people like to call it.

fredrik's got a micro pep at http://online.effbot.org

> Once we have (say) static, I think making the case expressions static
> by default would still cover all useful cases, and would allow us to
> diagnose duplicate cases reliably (which the if/elif chain semantics
> don't allow IIUC).

Making case expressions default static would be very surprising to users
because of the restrictions placed by static. For instance 'case in a',
will not support containers which have a custom __contains__ method. It
will also not support containers like lists, and sets because they are
mutable. IMHO this doesn't feel very pythonic.

Instead if we redefine the goal of the switch statement to be 'ease of
expression' rather than 'optimization', then it can just be used as a
concise alternative to if-elif chains, and we can make 'case in a' work
with all containers where a regular 'in' statement works *AND* still
give the possibility of fast lookup when the programmer wants, using
explicit static.

I feel programmer expressivity is more important, and default static
case expressions looks like premature optimization.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : http://mail.python.org/pipermail/python-dev/attachments/20060627/3349e311/attachment.pgp 

More information about the Python-Dev mailing list