A certainl part of an if() structure never gets executed.

Jussi Piitulainen jpiitula at ling.helsinki.fi
Fri Jun 14 08:40:00 EDT 2013


Nick the Gr33k writes:
> On 14/6/2013 12:21 μμ, Jussi Piitulainen wrote:
> > Nick the Gr33k writes:
> >> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
> >>
> >>>>>> 'Parker' and 'May' and '2001'
> >>> '2001'
> >>
> >> But why?
> >>
> >> that expression should return True since all stings are not empty.
> >
> > It returns a value that counts as true in a conditional statement or
> > expression:
> 
> When a look at ('Parker' and 'May' and '2001') i can't help it but
> interpret it as:
> 
> Return True if  'Parker' is not an empty string AND 'May' is not an
> empty string AND'2001'  is not an empty string.

Nah. That expression would be:

  True if ('Parker' != '' and 'May' != '' and '2001' != '') else False

The one at hand is more like: 'Parker' if 'Parker' does not count as
true, else 'May' if 'May' does not count as true, else '2001' (which
counts as true if it counts as true, else it counts as false ...).

> i just don't understand why it returns back the last value instead.

I suppose the value can be useful, and there is generally no harm in
it. But why not adjust your expectations to the reality? You can still
ask why.

This behaviour of the /and/ and /or/ was used to simulate the
conditional expression (_ if _ else _) before the latter was in the
language.

> >>>> '' and whatever
> > ''
> 
> Why does it return th first object back
> isn't it like saying False and True and resulting in False?
> 
> Please put it in else word how Python unerstand that.

That would be: '' if '' counts as false, else whatever.

And yes, when the first expression determines the value, the second
expression is not evaluated. /or/ is similar.



More information about the Python-list mailing list