[Tutor] “has a value of True” versus “evaluates true” (was: don't understand iteration)

Steven D'Aprano steve at pearwood.info
Thu Nov 13 00:45:18 CET 2014


On Tue, Nov 11, 2014 at 10:24:00AM +1100, Ben Finney wrote:
> "Clayton Kirkwood" <crk at godblessthe.us> writes:
> 
> > Also of confusion, the library reference says:
> >
> > Match objects always have a boolean value of True. Since match() and
> > search() return None when there is no match, you can test whether there was
> > a match with a simple if statement:
> >
> > match = re.search(pattern, string)
> > if match:
> >     process(match)
> 
> The documentation is incorrect, as you point out: “have a boolean value
> of True” implies that the value is identical to the built-in ‘True’
> constant, which is never the case for these objects.

I disagree with that interpretation. "X has a boolean value of 
True" does not imply that X is necessarily True, but only that bool(X) is 
True. Is the same way we might say:

    Lists have a string representation with [ and ] delimiters 
    and , item separators

I trust that you wouldn't interprete that as meaning that lists *are* 
strings.

If the documentation said that match objects *are* the boolean value 
True, then you would be correct, but it doesn't. Your interpretation 
confuses "has" for "is".

To put it another way, "X has a boolean value of True" is synonymous 
with any of these alternative ways of saying the same thing:

"X evaluates like True in a boolean context"

"X is truthy"

"X is a truthy value"

"X is a true-like value"

"X is equivalent to the bool True in boolean contexts"

"X has the quality of being interpreted as true in boolean contexts"

"bool(X) returns True"

and many other ways of saying the same thing.



> Instead, the passage above should say “evaluates true in a boolean
> context”.
> 
> Would you be so kind as to report a bug to that effect
> <URL:http://bugs.python.org/>?

I shouldn't bother if I were you :-)



-- 
Steven


More information about the Tutor mailing list