Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79523 > unrolled thread
| Started by | Simon Kennedy <sffjunkie@gmail.com> |
|---|---|
| First post | 2014-10-23 07:30 -0700 |
| Last post | 2014-10-23 11:24 -0400 |
| Articles | 10 — 8 participants |
Back to article view | Back to comp.lang.python
Truthiness Simon Kennedy <sffjunkie@gmail.com> - 2014-10-23 07:30 -0700
Re: Truthiness Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-23 08:45 -0600
Re: Truthiness Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-24 11:42 +1100
Re: Truthiness Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-10-23 16:47 +0200
Re: Truthiness Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-23 17:01 +0200
Re: Truthiness Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-23 16:07 +0000
Re: Truthiness Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-23 16:49 +0200
Re: Truthiness Skip Montanaro <skip.montanaro@gmail.com> - 2014-10-23 09:51 -0500
Re: Truthiness Simon Kennedy <sffjunkie@gmail.com> - 2014-10-23 07:56 -0700
Re: Truthiness random832@fastmail.us - 2014-10-23 11:24 -0400
| From | Simon Kennedy <sffjunkie@gmail.com> |
|---|---|
| Date | 2014-10-23 07:30 -0700 |
| Subject | Truthiness |
| Message-ID | <61f0b04c-b827-4c03-b55d-4d5bd85b56f6@googlegroups.com> |
Just out of academic interest, is there somewhere in the Python docs where the following is explained?
>>> 3 == True
False
>>> if 3:
print("It's Twue")
It's Twue
i.e. in the if statement 3 is True but not in the first
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-10-23 08:45 -0600 |
| Message-ID | <mailman.15110.1414075591.18130.python-list@python.org> |
| In reply to | #79523 |
On Thu, Oct 23, 2014 at 8:30 AM, Simon Kennedy <sffjunkie@gmail.com> wrote:
> Just out of academic interest, is there somewhere in the Python docs where the following is explained?
https://docs.python.org/3/reference/expressions.html#booleans
>>>> 3 == True
> False
>>>> if 3:
> print("It's Twue")
>
> It's Twue
>
> i.e. in the if statement 3 is True but not in the first
No, 3 is merely true, not True. True is just the name of a particular
singleton object that is also true.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-10-24 11:42 +1100 |
| Message-ID | <5449a089$0$12988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #79527 |
Ian Kelly wrote: > No, 3 is merely true, not True. True is just the name of a particular > singleton object that is also true. Sometimes I distinguish between "true" and "True", where True is the canonical boolean true object, but I prefer to refer to "true-like", "true-ish", or "truthy" objects as a shorthand for "evaluates the same as True in a boolean context" Likewise for "false-like, false-ish, falsey" for objects which evaluate the same as False in a boolean context. It should also be pointed out that, for built-ins and the standard library at least, a good distinction to make is that representations of Nothing (e.g. None, empty string, empty list, zero, empty set, etc.) are falsey, while representations of Something (e.g. non-empty strings, non-empty lists, numbers other than zero, non-empty sets, etc.) are truthy. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Alain Ketterlin <alain@dpt-info.u-strasbg.fr> |
|---|---|
| Date | 2014-10-23 16:47 +0200 |
| Message-ID | <87bnp23ksg.fsf@dpt-info.u-strasbg.fr> |
| In reply to | #79523 |
Simon Kennedy <sffjunkie@gmail.com> writes:
> Just out of academic interest, is there somewhere in the Python docs where the following is explained?
>
>>>> 3 == True
> False
>>>> if 3:
> print("It's Twue")
>
> It's Twue
>
> i.e. in the if statement 3 is True but not in the first
https://docs.python.org/2/reference/compound_stmts.html#the-if-statement
says: "The if statement [...] selects exactly one of the suites by
evaluating the expressions one by one until one is found to be true (see
section Boolean operations for the definition of true and false)"
and then:
https://docs.python.org/2/reference/expressions.html#booleans
says: "In the context of Boolean operations, and also when expressions
are used by control flow statements, the following values are
interpreted as false: False, None, numeric zero of all types, and empty
strings and containers (including strings, tuples, lists, dictionaries,
sets and frozensets). All other values are interpreted as true."
(links are to the 2.7 version of the reference manual, I think not much
has changed in 3.* versions.)
-- Alain.
[toc] | [prev] | [next] | [standalone]
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Date | 2014-10-23 17:01 +0200 |
| Message-ID | <mailman.15113.1414076484.18130.python-list@python.org> |
| In reply to | #79528 |
On 10/23/2014 04:47 PM, Alain Ketterlin wrote:
> Simon Kennedy <sffjunkie@gmail.com> writes:
>
>> Just out of academic interest, is there somewhere in the Python docs where the following is explained?
>>
>>>>> 3 == True
>> False
>>>>> if 3:
>> print("It's Twue")
>>
>> It's Twue
>>
>> i.e. in the if statement 3 is True but not in the first
>
> https://docs.python.org/2/reference/compound_stmts.html#the-if-statement
>
> says: "The if statement [...] selects exactly one of the suites by
> evaluating the expressions one by one until one is found to be true (see
> section Boolean operations for the definition of true and false)"
>
Exactly, but in
if 3 == True:
the expression is 3==True , in which 3 and True compare unequal, thus,
the expression is false.
On the other hand, in
if 3:
the expression to evaluate is just the int object and the rules below apply.
> and then:
>
> https://docs.python.org/2/reference/expressions.html#booleans
>
> says: "In the context of Boolean operations, and also when expressions
> are used by control flow statements, the following values are
> interpreted as false: False, None, numeric zero of all types, and empty
> strings and containers (including strings, tuples, lists, dictionaries,
> sets and frozensets). All other values are interpreted as true."
>
> (links are to the 2.7 version of the reference manual, I think not much
> has changed in 3.* versions.)
>
> -- Alain.
>
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-10-23 16:07 +0000 |
| Message-ID | <m2b93i$u0g$3@dont-email.me> |
| In reply to | #79528 |
On Thu, 23 Oct 2014 16:47:27 +0200, Alain Ketterlin wrote: > says: "In the context of Boolean operations, and also when expressions > are used by control flow statements, the following values are > interpreted as false: False, None, numeric zero of all types, and empty > strings and containers (including strings, tuples, lists, dictionaries, > sets and frozensets). All other values are interpreted as true." Yep, and I expect this to bite S4H shortly, when he can't understand why the following are not all of the same truthiness: 0 (falsey - numeric 0) [] (falsey - empty set) "" (falsey - empty string) [""] (truthy - non empty set) [0] (truthy - non empty set) -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Date | 2014-10-23 16:49 +0200 |
| Message-ID | <mailman.15111.1414075725.18130.python-list@python.org> |
| In reply to | #79523 |
On 10/23/2014 04:30 PM, Simon Kennedy wrote:
> Just out of academic interest, is there somewhere in the Python docs where the following is explained?
>
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
>>>> 3 == True
> False
as opposed to:
https://docs.python.org/3/library/stdtypes.html#comparisons
>>>> if 3:
> print("It's Twue")
>
> It's Twue
>
> i.e. in the if statement 3 is True but not in the first
>
Here is the misunderstanding: it is not true that 3 is True in the if
example, but it's evaluated as True in a boolean context. Illustration:
>>> 3 == True
False
>>> bool(3) == True
True
>>> bool(3) is True
True
If you combine if and a comparison, it behaves like in your first example:
>>>
if 3 == True:
print('True')
else:
print('False')
False
[toc] | [prev] | [next] | [standalone]
| From | Skip Montanaro <skip.montanaro@gmail.com> |
|---|---|
| Date | 2014-10-23 09:51 -0500 |
| Message-ID | <mailman.15112.1414075909.18130.python-list@python.org> |
| In reply to | #79523 |
[Multipart message — attachments visible in raw view] — view raw
On Thu, Oct 23, 2014 at 9:30 AM, Simon Kennedy <sffjunkie@gmail.com> wrote: > Just out of academic interest, is there somewhere in the Python docs where > the following is explained? Yes: https://docs.python.org/3/library/stdtypes.html#truth-value-testing https://docs.python.org/2.7/library/stdtypes.html#truth-value-testing Skip
[toc] | [prev] | [next] | [standalone]
| From | Simon Kennedy <sffjunkie@gmail.com> |
|---|---|
| Date | 2014-10-23 07:56 -0700 |
| Message-ID | <074b949d-9999-42b9-9136-6e32d1cee922@googlegroups.com> |
| In reply to | #79523 |
Thanks everyone. That's a thorough enough explanation for me.
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2014-10-23 11:24 -0400 |
| Message-ID | <mailman.15114.1414077886.18130.python-list@python.org> |
| In reply to | #79533 |
On Thu, Oct 23, 2014, at 10:56, Simon Kennedy wrote: > Thanks everyone. That's a thorough enough explanation for me. You should know, though, that numeric values equal to 1 (and 0 for False) _are_ == True. This works for dictionary keys, array indexes, etc. The bool type is actually a subclass of int. Mathematical operations on it will simply return an int, or float for division - logical operations and bitwise and/or/xor return bool (bitwise not returns an int because it returns the values -1 and -2) So checking if something is == True is the same as checking if it's == 1, and checking if it's == False is the same as checking if it's == 0.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web