[Python-ideas] "else" expression ":"

Shane Green shane at umbrellacode.com
Sun Apr 14 13:48:08 CEST 2013


I think that, if something special were added for this case, it may be best for it to be not overload the so incredibly well defined through the ages if/elif/ or else clauses, perhaps adding something new, like: 

elifassert val==0:
	retur…

…you know, I've got say, I don't really recall finding myself in this situation very often in the first place.  It seems like the most productive solution might be to plant an easter egg that suggests some of these alternative approaches!







Shane Green 
www.umbrellacode.com
408-692-4666 | shane at umbrellacode.com

On Apr 14, 2013, at 3:31 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On Sun, Apr 14, 2013 at 4:24 AM, Peter Norvig <peter at norvig.com> wrote:
>> 
>> 
>> 
>> Beginners will often write code like this:
>> 
>> if val > 0:
>>    return +1
>> elif val < 0:
>>    return -1
>> elif val == 0:
>>    return 0
>> 
>> Now if you did this in Java, the compiler would produce an error saying that
>> there is an execution path that does not return a value. Python does not
>> give an error message, but it would be considered more idiomatic (and
>> slightly more efficient) to have just "else:" in the third clause.
>> 
>> Here's an idea to address this.  What do you think of the syntax
>> 
>>     "else" expression ":"
>> 
>> for example:
>> 
>> if val > 0:
>>    return +1
>> elif val < 0:
>>    return -1
>> else val == 0:
>>    return 0
>> 
>> with the interpretation:
>> 
>> if val > 0:
>>    return +1
>> elif val < 0:
>>    return -1
>> else:
>>    assert val == 0
>>    return 0
>> 
>> I have to say, I'm uncertain.  I'm not sure this is even a good idea at all,
>> and I'm not sure if it should translate into  "assert expression" or whether
>> it should be "if not expression: raise ValueError". What do you think?
> 
> I think the difference between:
> 
>  if val > 0:
>    return +1
>  elif val < 0:
>    return -1
>  else val == 0:
>    return 0
> 
> and:
> 
>  if val > 0:
>    return +1
>  elif val < 0:
>    return -1
>  elif val == 0:
>    return 0
> 
> is far too subtle to be helpful. If an if/elif chain is expected to be
> exhaustive and you want to ensure it remains that way during ongoing
> maintenance, you can already do:
> 
>  if val > 0:
>    return +1
>  elif val < 0:
>    return -1
>  elif val == 0:
>    return 0
>  else:
>    raise RuntimeError("Unhandled input: {!r:100}".format(val))
> 
> (with the "else:" being optional if this is the last statement in a function)
> 
> In general, though, Python already decided on its answer to this
> question by always allowing a final "return None" to be implicit, even
> when there are explicit return statements elsewhere in the function.
> 
> Cheers,
> Nick.
> 
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130414/e6261666/attachment.html>


More information about the Python-ideas mailing list