Quantcast
Channel: Top-rated recipes
Viewing all articles
Browse latest Browse all 20

Readable switch construction without lambdas or dictionaries (Python)

$
0
0

Python recipe 410692 by Brian Beck (extending). Revision 8.

Python's lack of a 'switch' statement has garnered much discussion and even a PEP. The most popular substitute uses dictionaries to map cases to functions, which requires lots of defs or lambdas. While the approach shown here may be O(n) for cases, it aims to duplicate C's original 'switch' functionality and structure with reasonable accuracy.


Viewing all articles
Browse latest Browse all 20

Trending Articles