On Fri, Nov 1, 2019 at 12:52 AM Andrew Barnert via Python-ideas <python-ideas@python.org> wrote: > Also, there are many places in the Python data model where you have to raise > an exception, such as StopIteration; there are no places where you need to > print. And if you need output and print is too slow, you can always go below; > there’s no way to raise an exception without raise. > > And what’s the advantage? > > If we need raise inside an expression without extra overhead, this doesn’t > solve the problem; we’d need a raise expression (similar to the yield > expression), not a raise function. Which has the advantage of not breaking > any existing code (except for code that deals with AST nodes or tokens). >
If "raise in an expression context" is an important-enough feature, making raise an expression would be the most logical way to do it (as you say, similar to a yield expression). Wouldn't need a future directive or anything. When print was turned into a function, part of the justification was that you could then shadow the builtin with your own. If anyone has a use-case for shadowing the raise() function, I've yet to hear it, although that's not to say there isn't one :) But I don't think the overhead of a function call is worth the cost. If raise were to become an expression like yield, it would still be able to use the syntax "raise Exception from None" rather than having to use a function call, which means the backward incompatibility would be minimal. But I'm still -0 on making that change. It feels wrong to have an expression that can never have any value. If you write "spam = raise Exception()", what is spam set to? Well, nothing, because we never do that assignment. There are already lots of things you can't do with lambda functions, and we don't need to make assignment an expression just for the sake of this: transaction.side_effect = lambda: counter["foo"] += 1 If this is really such a problem for people, I'd rather push the other direction, and allow the 'def' statement to be able to assign to anything: def transaction.side_effect(): counter["foo"] += 1 It's no less "weird" than making raise an expression, and potentially has other uses too. I'm sure there'll be a ton of bikeshedding (what should __name__ and __qualname__ be?), but that can go in its own thread, if people need a cleaner solution to this problem. (You can even put that on one line, if you want it to feel more like a lambda function!) ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/I3KBBNHT4PPWHUWZVEX2IXUYVVXCDBDR/ Code of Conduct: http://python.org/psf/codeofconduct/