I'm basically attempting py2/3 compatibility by trying to add a a fullmatch
method to Python2 re
compiled patterns.
I've seen https://stackoverflow.com/a/2982/2694385 on how to add an attribute to an existing instance. I'm creating the method as given in https://stackoverflow.com/a/30212799/2694385.
Example code
import re
import six
regex = re.compile('some pattern') # Better if we keep this same
if six.PY2:
def fullmatch(self_regex, string, flags=0):
return self_regex.match(string, flags=flags)
regex = re.compile(r'(?:' + regex.pattern + r')\Z', flags=regex.flags)
import types
bound = types.MethodType(fullmatch, regex)
# AttributeError here. None of the following three lines work
regex.fullmatch = bound
regex.__setattr__('fullmatch', bound)
setattr(regex, 'fullmatch', bound)