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.
Created on 2014年11月11日 01:26 by crkirkwood, last changed 2022年04月11日 14:58 by admin. This issue is now closed.
| Messages (6) | |||
|---|---|---|---|
| msg230994 - (view) | Author: Clayton Kirkwood (crkirkwood) | Date: 2014年11月11日 01:26 | |
Documentation 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)
What happens:
blah = <_sre.SRE_Match object; span=(0, 28), match='<BR>Nov. 10, 08:16:09 PM EST'>
if blah == True:
print("True")
if blah:
print('blah True')
blah True
///
Blah is not True
One suggestion: instead, the passage above should say "evaluates true in a boolean context".
|
|||
| msg230996 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2014年11月11日 01:42 | |
That's what "have a boolean value of True" means. (ie: bool(<matchobject>) is True). I'm neutral on whether or not it is worth changing the wording. |
|||
| msg231010 - (view) | Author: Georg Brandl (georg.brandl) * (Python committer) | Date: 2014年11月11日 09:12 | |
"evaluates true" should not be used in any case, the objects do not equal to True in any case. The phrase "is considered true in a boolean context" is already in the docs and could be used here too. |
|||
| msg231090 - (view) | Author: Ben Finney (bignose) | Date: 2014年11月12日 17:45 | |
The current wording of the passage "Match objects always have a boolean value of True" implies that the value compares equal to the ‘True’ constant. That implication is incorrect. I disagree with R. David Murray; if we want to say that a value is considered true *in a boolean context*, that's very different from saying it has the "True" value. Georg, "evaluates true in a boolean context" has the meaning you're seeking; it is chosen precisely because it does *not* imply equality to the True constant. |
|||
| msg231185 - (view) | Author: Terry J. Reedy (terry.reedy) * (Python committer) | Date: 2014年11月14日 21:03 | |
David is correct that the current phrasing is correct. The phase 'x has a boolean value of True' means 'bool(x) is True', which is always true for match objects, as well as for non-zero numbers, non-empty collections, and many other things. This does *not* imply equality between the object and its boolean value. In fact, nearly all objects are not equal to their boolean value. Clayton could just as well as have written "blah = 'a'" or "blah = 1 + 1j" and gotten the name non-surprising result. There is nothing special about boolean values in this respect. The string value of x is str(x) and in general, x != str(x). (This also sometimes confuses people.) Similarly, if x has an integral value int(x), it does not necessarily equal that value: int(3.1459) != 3. I think the doc is fine as is. The fact that "3 is considered to be '3' in a display context" does not mean that we do not write "the string value of 3 is '3'". It is fundamental to Python that essentially all objects o have a string value str(o) and a boolean value bool(o) and that those mappings are sometimes used automatically for display and logic. |
|||
| msg231187 - (view) | Author: Clayton Kirkwood (crkirkwood) | Date: 2014年11月14日 21:25 | |
Cool >-----Original Message----- >From: Terry J. Reedy [mailto:report@bugs.python.org] >Sent: Friday, November 14, 2014 1:04 PM >To: crk@godblessthe.us >Subject: [issue22843] doc error: 6.2.4. Match Objects > > >Terry J. Reedy added the comment: > >David is correct that the current phrasing is correct. The phase 'x has >a boolean value of True' means 'bool(x) is True', which is always true >for match objects, as well as for non-zero numbers, non-empty >collections, and many other things. This does *not* imply equality >between the object and its boolean value. In fact, nearly all objects >are not equal to their boolean value. Clayton could just as well as >have written "blah = 'a'" or "blah = 1 + 1j" and gotten the name non- >surprising result. > >There is nothing special about boolean values in this respect. The >string value of x is str(x) and in general, x != str(x). (This also >sometimes confuses people.) Similarly, if x has an integral value >int(x), it does not necessarily equal that value: int(3.1459) != 3. > >I think the doc is fine as is. The fact that "3 is considered to be '3' >in a display context" does not mean that we do not write "the string >value of 3 is '3'". It is fundamental to Python that essentially all >objects o have a string value str(o) and a boolean value bool(o) and >that those mappings are sometimes used automatically for display and >logic. > >---------- >nosy: +terry.reedy > >_______________________________________ >Python tracker <report@bugs.python.org> ><http://bugs.python.org/issue22843> >_______________________________________ |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:58:10 | admin | set | github: 67032 |
| 2021年09月10日 18:29:51 | iritkatriel | set | status: open -> closed resolution: not a bug stage: resolved |
| 2014年11月14日 21:25:15 | crkirkwood | set | messages: + msg231187 |
| 2014年11月14日 21:03:34 | terry.reedy | set | nosy:
+ terry.reedy messages: + msg231185 |
| 2014年11月12日 17:45:55 | bignose | set | nosy:
+ bignose messages: + msg231090 |
| 2014年11月11日 09:12:11 | georg.brandl | set | nosy:
+ georg.brandl messages: + msg231010 |
| 2014年11月11日 01:42:14 | r.david.murray | set | type: resource usage -> behavior messages: + msg230996 nosy: + r.david.murray |
| 2014年11月11日 01:26:03 | crkirkwood | create | |