[issue29323] Wrong documentation (Library) for unicode and str comparison
New submission from RK-5wWm9h:
PROBLEM (IN BRIEF):
In the currently published 2.7.13 The Python Standard Library (Library Reference manual) section 5.6 "Sequence Types" (https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-l...):
"to compare equal, ... the two sequences must be of the same type"
This an *incorrect (and misleading) statement*, for the unicode and str case.
PROPOSED FIX:
Current full paragraph:
"Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.)"
Proposed replacement text:
"Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences
must be of the same type and have the same length. (Unicode and str are treated as the same type here; for full details see Comparisons in the language reference.)"
DETAILS, JUSTIFICATION, CORRECTNESS, ETC:
The current incorrect text is really misleading.
The behaviour that a str and a unicode object -- despite being objects of different types -- may compare equal, is explicitly stated in the 2.7.13 The Python Language Reference manual, section 5.9 "Comparisons" (https://docs.python.org/2/reference/expressions.html#comparisons):
"* Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord()) of their characters. Unicode and 8-bit strings are fully interoperable in this behavior. [4]"
(Aside: Incidentally an earlier paragraph in the Language Ref fails to cover the unicode and str case; see separately filed bug Issue 29321.)
----------
assignee: docs@python
components: Documentation
messages: 285792
nosy: RK-5wWm9h, docs@python
priority: normal
severity: normal
status: open
title: Wrong documentation (Library) for unicode and str comparison
type: behavior
versions: Python 2.7
_______________________________________
Python tracker
R. David Murray added the comment:
Unicode and string *are* of the same type: basestring. This is a specific example of the liskov substitution principle, so I don't think it should be called out explicitly in this section.
----------
nosy: +r.david.murray
_______________________________________
Python tracker
R. David Murray added the comment:
As per your other issue, though, the real issue is that the two objects must be *comparable*, not that they be of the same type, and the language should probably be updated to reflect that.
----------
_______________________________________
Python tracker
Martin Panter added the comment:
If you read the whole paragraph carefully, I don't think it is too misleading. "In particular, tuples and lists . . ." suggests the author was just trying to say that a tuple never compares equal to a list. Maybe we just need to make that more obvious?
However there are other problems in this part of the reference about comparing different types. See Issue 22000, about the earlier section on Comparisons of built-in types.
----------
nosy: +martin.panter
_______________________________________
Python tracker
R. David Murray added the comment:
That's a good point, I think that is exactly the issue with that paragraph.
----------
_______________________________________
Python tracker
Serhiy Storchaka
participants (4)
-
Martin Panter
-
R. David Murray
-
RK-5wWm9h
-
Serhiy Storchaka