I understand that the SequenceMatcher's ratio method does not guarantee that SequenceMatcher(None, a, b).ratio() == SequenceMatcher(None, b, a).ratio(). Below is a counterexample:
# Example from https://mail.python.org/pipermail/python-list/2010-November/593063.html
>>> SequenceMatcher(None, 'BRADY', 'BYRD').ratio()
0.6666666666666666
>>> SequenceMatcher(None, 'BYRD', 'BRADY').ratio()
0.4444444444444444
I was recently solving a problem that required a textual similarity ratio function and I wrongly assumed that SequenceMatcher treated both input strings symmetrically, which was an extremely difficult bug to find, especially because for many simple tests, the ratio IS symmetric:
I would like to see a clearer warning of this asymmetry in the documentation for the difflib module. Perhaps something like
.. note::
Caution: The result of a :meth:`ratio` call is *NOT* symmetric with
respect to the order of the arguments. For instance::
>>> SequenceMatcher(None, 'brady', 'byrd').ratio()
0.6666666666666666
>>> SequenceMatcher(None, 'byrd', 'brady').ratio()
0.4444444444444444
Without such a note near the ratio methods' documentations, it is far too easy to google for a Python stdlib functionality for computing text similarity, skip straight to the ratio method, look at the examples given, try some of your own simple examples, and accidentally convince oneself that this symmetry exists.
New changeset e9cbcd0 by Miss Islington (bot) (sweeneyde) in branch 'master': bpo-37004: Documented asymmetry of string arguments in difflib.SequenceMatcher for ratio method (GH-13482) e9cbcd0
New changeset 1a3a40c by Terry Jan Reedy (Miss Islington (bot)) in branch '3.8': bpo-37004: Documented asymmetry of string arguments in difflib.SequenceMatcher for ratio method (GH-13482) (bpo-15157) 1a3a40c
New changeset 7dafbe8 by Terry Jan Reedy (Miss Islington (bot)) in branch '3.7': bpo-37004: Documented asymmetry of string arguments in difflib.SequenceMatcher for ratio method (GH-13482) (bpo-15158) 7dafbe8
Activity
sweeneyde commented on May 22, 2019
I understand that the SequenceMatcher's ratio method does not guarantee that SequenceMatcher(None, a, b).ratio() == SequenceMatcher(None, b, a).ratio(). Below is a counterexample:
# Example from https://mail.python.org/pipermail/python-list/2010-November/593063.html >>> SequenceMatcher(None, 'BRADY', 'BYRD').ratio() 0.6666666666666666 >>> SequenceMatcher(None, 'BYRD', 'BRADY').ratio() 0.4444444444444444I was recently solving a problem that required a textual similarity ratio function and I wrongly assumed that SequenceMatcher treated both input strings symmetrically, which was an extremely difficult bug to find, especially because for many simple tests, the ratio IS symmetric:
>>> SequenceMatcher(None, 'apple', 'banana').ratio() 0.18181818181818182 >>> SequenceMatcher(None, 'banana', 'apple').ratio() 0.18181818181818182I would like to see a clearer warning of this asymmetry in the documentation for the difflib module. Perhaps something like
Caution: The result of a :meth:`ratio` call is *NOT* symmetric with respect to the order of the arguments. For instance:: >>> SequenceMatcher(None, 'brady', 'byrd').ratio() 0.6666666666666666 >>> SequenceMatcher(None, 'byrd', 'brady').ratio() 0.4444444444444444Without such a note near the ratio methods' documentations, it is far too easy to google for a Python stdlib functionality for computing text similarity, skip straight to the ratio method, look at the examples given, try some of your own simple examples, and accidentally convince oneself that this symmetry exists.
miss-islington commented on Aug 7, 2019
New changeset e9cbcd0 by Miss Islington (bot) (sweeneyde) in branch 'master':
bpo-37004: Documented asymmetry of string arguments in difflib.SequenceMatcher for ratio method (GH-13482)
e9cbcd0
terryjreedy commented on Aug 7, 2019
New changeset 1a3a40c by Terry Jan Reedy (Miss Islington (bot)) in branch '3.8':
bpo-37004: Documented asymmetry of string arguments in difflib.SequenceMatcher for ratio method (GH-13482) (bpo-15157)
1a3a40c
terryjreedy commented on Aug 7, 2019
New changeset 7dafbe8 by Terry Jan Reedy (Miss Islington (bot)) in branch '3.7':
bpo-37004: Documented asymmetry of string arguments in difflib.SequenceMatcher for ratio method (GH-13482) (bpo-15158)
7dafbe8