On Sat, Jan 31, 2009 at 4:36 AM, AJ Ostergaard <a...@cubbyhole.net> wrote:
I'm not suggesting it's not operating as advertised - I'm suggesting the 'advertising' is slightly sguiffy if you catch my drift. I guess it's just me that finds it slightly counter intuitive. Surely intuitively the _expression_ is "and" and therefore should always return a boolean?

Boolean operators just aren't guaranteed to return a boolean /type/ -- but instead a boolean /_expression_/, that if evaluated is true or false. The difference is important -- and although it might be partly for historical reasons (after all, there was no boolean /type/ until Python 2.3. That's not really all that long ago), it also has quite a few useful properties that aren't necessecarily immediately obvious but are used in a variety of places.

A common one is the and/or "ternary" _expression_ that pre-dates the addition of conditional expressions to Python 2.5:

>>> test = True
>>> test and "Apples" or "Oranges"
'Apples'
>>> test = False
>>> test and "Apples" or "Oranges"
'Oranges'

In Python 2.5., that can be rewritten to:

>>> test = True
>>> "Apples" if test else "Oranges"
'Apples'
>>> test = False
>>> "Apples" if test else "Oranges"
'Oranges'

Its part of why you aren't supposed to compare boolean results/tests/expressions against True or False directly usually (besides /just/ style). You do:

if _expression_:
   print "Was true!"

Not:

if _expression_ is True:
    print "Was true!"

--Stephen

Attachment: signature.asc
Description: OpenPGP digital signature

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to