[Python-ideas] except expression
Oscar Benjamin
oscar.j.benjamin at gmail.com
Wed Feb 19 13:57:37 CET 2014
On 19 February 2014 07:06, Chris Angelico <rosuav at gmail.com> wrote:
> On Wed, Feb 19, 2014 at 5:10 PM, Chris Angelico <rosuav at gmail.com> wrote:
>
> Current PEP draft:
>
> https://raw2.github.com/Rosuav/ExceptExpr/master/pep-0463.txt
It looks good to me.
I think I most often use the ternary if/else as part of a larger
expression such as a dict display. I can imagine that the same would
apply here and it might be good to add an example of this.
I don't know how common a pattern it is in other people's code but
something like:
header = {
'param1': foo,
'param2': bar,
...
}
try:
header['paramN'] = load_from_file(baz, spam)
except OSError:
header['paramN'] = ''
In this case your proposed syntax would make it possible to write the
whole thing as a single expression. Without the syntax you'd probably
need to write an additional function to get the same effect:
def load_from_file_default_None(baz, spam):
try:
return load_from_file(baz, spam)
except OSError:
return None
Oscar
More information about the Python-ideas
mailing list