This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: and is not a logical conjugation
Type: Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, the_iain
Priority: normal Keywords:

Created on 2011-04-01 14:22 by the_iain, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg132740 - (view) Author: Iain Henderson (the_iain) Date: 2011-04-01 14:22
The documentation here: http://docs.python.org/library/stdtypes.html
indicates that and operates as such
{if x:
    return x
else:
    return y}

to be a logical conjugation it should function as
{if x:
    if y:
        return True
return False}

The it is now (False and True) will return True.  Basic logic asserts that this is not the case.
msg132741 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-04-01 14:35
The doc[0] says:
"""
x and y: if x is false, then x, else y
"""
Boolean operators in Python always return one of the two values (rather than True/False), and they are also short-circuit operators, so:
  * if x is false, the whole expression is false regardless of the value of y, so x is returned without evaluating y;
  * if x is true, y could be either:
    * true: so the whole expression is true and y is returned;
    * false: so the whole expression is false and y is returned;

>>> '' and True
''
>>> True and ''
''
>>> True and 15
15

The behavior matches the documentation and (False and True) returns False, because x (False in this case) is false.

[0]: http://docs.python.org/library/stdtypes.html#boolean-operations-and-or-not
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55946
2011-04-01 14:35:00ezio.melottisetstatus: open -> closed

nosy: + ezio.melotti
messages: + msg132741

resolution: not a bug
stage: resolved
2011-04-01 14:22:55the_iaincreate