[Python-Dev] Issue 2986: difflib.SequenceMatcher is partly broken

Nick Coghlan ncoghlan at gmail.com
Wed Jul 14 15:45:41 CEST 2010


On Wed, Jul 14, 2010 at 10:38 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 4. I normally dislike global flags, but this is one time it might be
> less-worse than the alternatives.
>
> Modify SequenceMatcher to test for the value of a global flag,
> defaulting to False if it doesn't exist.
>
> try:
>    disable = disable_heuristic
> except NameError:
>    disable = False
> if disable:
>    # use the new, correct behaviour
> else:
>    # stick to the old, backwards-compatible but wrong behaviour
>
>
>
> The flag will only exist if the caller explicitly creates it:
>
> import difflib
> difflib.disable_heuristic = True
>
> On 2.7.0 and older versions, creating the flag won't do anything useful,
> but nor will it cause an exception. It will be harmless.
>
> I think an explicit flag is better than relying on magic behaviour
> triggered by "unlikely" input.

Why make it a global? A property on the SequenceMatcher object should
be able to do the right thing.

@property
def use_autojunk(self):
    if self._use_autojunk is None:
        raise AttributeError("...") # Same error as 2.7.0 would raise
    return self._use_autojunk

@use_autojunk.set
def use_autojunk(self, val):
    self._use_autojunk = bool(val)

Then use the self._use_autojunk flag to decide whether or not to use
the heuristic on 2.7.1+.

Code that sets the flag would behave the same on both 2.7.1+ and on
2.7.0, it would just fail to turn the heuristic off in 2.7.0.

Code that only read the flag without setting it would also behave the
same on the whole 2.7.x series (i.e. raising AttributeError if the
flag had not been explicitly set by the application)

A flag on the object rather than a per-call flag may actually be the
better API here anyway.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list