Eval of expr with 'or' and 'and' within

Benjamin Kaplan benjamin.kaplan at case.edu
Fri Jun 14 12:47:47 EDT 2013


On Jun 14, 2013 9:34 AM, "Michael Torrie" <torriem at gmail.com> wrote:
>
> On 06/14/2013 03:50 AM, Nick the Gr33k wrote:
> >  >>> print(name or month or year)
> > abcd
> >  >>> print(name and month and year)
> > ijkl
>
> Interesting.  I'd have thought a boolean expression would return True or
> False, not a string.  Learn something new every day.
>
>
>

Python didn't have a Boolean type for quite some time (2.2?). Until then,
True and False were ints. Since every object had a Boolean value, you
didn't need to turn the result into an integer. In an "and" clause, python
returns the first false value or the last value, because that will evaluate
to the correct Boolean value. In an "or" clause, python returns the first
true value or the last value. When Python finally got a Boolean type, no
one wanted to break backwards compatibility for this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130614/753c4008/attachment.html>


More information about the Python-list mailing list