[Python-ideas] switch statement as context manager?
Mathias Panzenböck
grosser.meister.morti at gmx.net
Mon Feb 10 19:14:30 CET 2014
You don't need a context manger here:
switch = lambda value: lambda other: value == other
case = switch(foobar)
if case(1): pass
if case(2): pass
But I don't think this is at all useful. Maybe if case does something more complex than == (e.g. isinstance for classes
and matching of regular expressions etc.). But than it's a bit of black magic. What if value and other both are classes
or regular expressions?
Other version:
case = foobar.__eq__
if case(1): pass
if case(2): pass
Am 2014-02-10 15:31, schrieb Sturla Molden:
> I've noticed that PyExt has a switch statement implemented as a context
> manager.
>
> with switch(foobar):
> if case(1): pass
> if case(2): pass
>
> Would this be something to consider for the standard lib, e.g. contextlib?
>
> Sturla
>
More information about the Python-ideas
mailing list