Feature suggestion -- return if true

Jason Swails jason.swails at gmail.com
Tue Apr 12 04:18:48 CEST 2011


On Mon, Apr 11, 2011 at 7:12 PM, James Mills
<prologic at shortcircuit.net.au>wrote:

>
> > Are you saying the two snippets above are equivalent?
>
> def foo(n):
>    x = n < 5
>    if x:
>        return x
>
> is functionally equivalent to:
>
> def foo(n):
>    return n < 5
>
>
This is only true if n < 5.  Otherwise, the first returns None and the
second returns False.

>>> def foo(n):
...     x = n < 5
...     if x: return x
...
>>> def foo1(n):
...     return n < 5
...
>>> foo(4)
True
>>> foo1(4)
True
>>> foo(6)
>>> foo1(6)
False
>>>

--Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110411/70410115/attachment.html>


More information about the Python-list mailing list