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
MacroPy has it: https://github.com/lihaoyi/macropy#pattern-matching =) On Mon, Feb 10, 2014 at 6:31 AM, Sturla Molden <sturla.molden@gmail.com>wrote:
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
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Probably not. When I wrote PyExt, it was with the sole idea that you can get around stuff by abusing Python's reflection mechanisms. Even the code for the switch statement is very hackish. I don't quite think that belongs in stdlib. On Mon, Feb 10, 2014 at 8:31 AM, Sturla Molden <sturla.molden@gmail.com>wrote:
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
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
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
See: https://gist.github.com/panzi/8924450 But again, I don't recommend to use this. Just to demonstrate what is already possible with the most simple constructs. Am 2014-02-10 19:14, schrieb Mathias Panzenböck:
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
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
It looks cooler. It also feels slightly less aggravating. On Mon, Feb 10, 2014 at 5:29 PM, Greg Ewing <greg.ewing@canterbury.ac.nz>wrote:
Sturla Molden wrote:
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
What advantage does this have over an if-else chain?
-- Greg
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
On Wed, Feb 12, 2014 at 2:22 AM, Ryan Gonzalez <rymg19@gmail.com> wrote:
It looks cooler. It also feels slightly less aggravating.
On Mon, Feb 10, 2014 at 5:29 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Sturla Molden wrote:
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
What advantage does this have over an if-else chain?
Since it fundamentally _is_ an if chain (without the elses), how does it feel less aggravating than one? ChrisA
Because it doesn't *look *like one. On Tue, Feb 11, 2014 at 9:25 AM, Chris Angelico <rosuav@gmail.com> wrote:
On Wed, Feb 12, 2014 at 2:22 AM, Ryan Gonzalez <rymg19@gmail.com> wrote:
It looks cooler. It also feels slightly less aggravating.
On Mon, Feb 10, 2014 at 5:29 PM, Greg Ewing <greg.ewing@canterbury.ac.nz
wrote:
Sturla Molden wrote:
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
What advantage does this have over an if-else chain?
Since it fundamentally _is_ an if chain (without the elses), how does it feel less aggravating than one?
ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
On 2/11/14 10:37 AM, Ryan Gonzalez wrote:
Because it doesn't /look /like one.
If it *is* one, it's a good thing to *look* like one. PS: could you bottom-post? It makes it easier to follow the thread. Thanks :) --Ned.
On Tue, Feb 11, 2014 at 9:25 AM, Chris Angelico <rosuav@gmail.com <mailto:rosuav@gmail.com>> wrote:
On Wed, Feb 12, 2014 at 2:22 AM, Ryan Gonzalez <rymg19@gmail.com <mailto:rymg19@gmail.com>> wrote: > It looks cooler. It also feels slightly less aggravating. > > > On Mon, Feb 10, 2014 at 5:29 PM, Greg Ewing <greg.ewing@canterbury.ac.nz <mailto:greg.ewing@canterbury.ac.nz>> > wrote: >> >> Sturla Molden wrote: >>> >>> 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 >> >> >> What advantage does this have over an if-else chain? >> >
Since it fundamentally _is_ an if chain (without the elses), how does it feel less aggravating than one?
ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org <mailto:Python-ideas@python.org> https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Tue, Feb 11, 2014 at 10:44 AM, Ned Batchelder <ned@nedbatchelder.com>wrote:
On 2/11/14 10:37 AM, Ryan Gonzalez wrote:
Because it doesn't *look *like one.
If it *is* one, it's a good thing to *look* like one.
PS: could you bottom-post? It makes it easier to follow the thread. Thanks :)
--Ned.
On Tue, Feb 11, 2014 at 9:25 AM, Chris Angelico <rosuav@gmail.com> wrote:
On Wed, Feb 12, 2014 at 2:22 AM, Ryan Gonzalez <rymg19@gmail.com> wrote:
It looks cooler. It also feels slightly less aggravating.
On Mon, Feb 10, 2014 at 5:29 PM, Greg Ewing < greg.ewing@canterbury.ac.nz> wrote:
Sturla Molden wrote:
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
What advantage does this have over an if-else chain?
Since it fundamentally _is_ an if chain (without the elses), how does it feel less aggravating than one?
ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
_______________________________________________ Python-ideas mailing listPython-ideas@python.orghttps://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Depends on whether or not you actually like the way it looks. P.S. Sorry, GMail top-posts by default. -- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
2014-02-11 17:44 GMT+01:00 Ned Batchelder <ned@nedbatchelder.com>: If it *is* one, it’s a good thing to *look* like one. well, you also get to remove the repeated variable name. but a real switch statement should take advantage of python’s hashing. the best we have is def _handle_spam(): vomit() def _handle_eggs(): yum() handlers = { 'spam': _handle_spam, 'eggs': _handle_eggs, } handlers[food]() which makes me _handle_spam(). ------------------------------ the problem of a real switch statement is apparently that there’s no good syntax: switch food case 'spam': ... is strange due to the missing colon. we have that nowhere in python. yet switch food: case 'spam': ... is strange because after the colon, everywhere else is an indentation. but switch food: case 'spam': ... is indentation overkill. nobody has found a good syntax yet.
C and C++ have: switch(value) { case 1: do_stuff; break; case 2: case 3: do_stuff_2(); break; default: break; } On Tue, Feb 11, 2014 at 10:58 AM, Philipp A. <flying-sheep@web.de> wrote:
2014-02-11 17:44 GMT+01:00 Ned Batchelder <ned@nedbatchelder.com>:
If it *is* one, it's a good thing to *look* like one.
well, you also get to remove the repeated variable name.
but a real switch statement should take advantage of python's hashing. the best we have is
def _handle_spam(): vomit() def _handle_eggs(): yum()
handlers = { 'spam': _handle_spam, 'eggs': _handle_eggs, }
handlers[food]()
which makes me _handle_spam(). ------------------------------
the problem of a real switch statement is apparently that there's no good syntax:
switch food case 'spam': ...
is strange due to the missing colon. we have that nowhere in python. yet
switch food: case 'spam': ...
is strange because after the colon, everywhere else is an indentation. but
switch food: case 'spam': ...
is indentation overkill. nobody has found a good syntax yet.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Ryan If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
On Tue, Feb 11, 2014 at 10:58 AM, Philipp A. <flying-sheep@web.de> wrote:
the problem of a real switch statement is apparently that there’s no good syntax
Probably worth mentioning the now 5+yo rejected PEP: http://www.python.org/dev/peps/pep-3103/ Skip
Skip Montanaro wrote:
On Tue, Feb 11, 2014 at 10:58 AM, Philipp A. <flying-sheep@web.de> wrote:
the problem of a real switch statement is apparently that there’s no good syntax
Last time this was discussed at length, the main sticking point seemed to be that people generally expect a switch statement to be implemented by something more efficient than a series of if-tests. But that's extremely difficult to do in Python except when all of the case values are literals, which is a very special situation. So in general, any switch statement in Python would just be pure syntactic sugar for an if-else chain, and that doesn't seem to be considered enough of an advantage to bother with. -- Greg
On Feb 11, 2014, at 8:58, "Philipp A." <flying-sheep@web.de> wrote:
2014-02-11 17:44 GMT+01:00 Ned Batchelder <ned@nedbatchelder.com>:
If it is one, it’s a good thing to look like one.
well, you also get to remove the repeated variable name.
Only by repeating the word "case" instead. Compare your syntax: switch some_long_expr(): if case(0): pass if case(1): pass if case(2): pass ... to what you can already write: case = some_long_expr() if case == 0: pass if case == 1: pass if case == 2: pass So you haven't saved anything at all. You've added more syntax, more characters, and an extra level of indentation. (Note that in more realistic uses, with real statements being controlled, your syntax indents them all twice instead of once.) If you really like using parens instead of equals, you can even do that: case = some_long_expr().__eq__ if case(0): pass if case(1): pass if case(2): pass Maybe you wanted your switch statement to automatically turn each if case into an elif. If so, then the existing alternative does require an extra "el" at the head of each line... But I think it's a lot clearer that at most one of these things will be executed that way. Also, of course, the elif chain is more flexible. Consider how hard it would be to write this as a switch: case = some_long_expr().__lt__ if case(0): pass elif case(1): pass elif case(2): pass Or maybe you wanted it to turn the chain of if case statements into a dict lookup or something. If so, that's even more limited, and a lot more magical, for very little benefit.
participants (10)
-
Andrew Barnert
-
Chris Angelico
-
Greg Ewing
-
Haoyi Li
-
Mathias Panzenböck
-
Ned Batchelder
-
Philipp A.
-
Ryan Gonzalez
-
Skip Montanaro
-
Sturla Molden