[Python-ideas] Give regex operations more sugar

Michel Desmoulin desmoulinmichel at gmail.com
Fri Jun 15 03:49:35 EDT 2018

Le 14/06/2018 à 07:29, Steven D'Aprano a écrit :
> On Wed, Jun 13, 2018 at 10:59:34PM +0200, Michel Desmoulin wrote:
>>> Attaching an entire module to a type is probably worse than
>>> adding a slew of extra methods to the type.
>> Not my point.
>> str.re would not be the re module, just a namespace where to group all
>> regex related string methods.
> That's what a module is :-)
> How would this work? If I say:
> "My string".re.match(...)
> if str.re is "just a namespace" how will the match function know the 
> string it is to operate on?

There are a lot of ways to do that. One possible way:

import re

class re_proxy:

    def __init__(self, string):
        self.string = string

    def match(self, pattern, flags):
        return re.match(pattern, self.string, flags)


def re(self):
    return re_proxy(self)

More information about the Python-ideas mailing list