[Python-ideas] Give regex operations more sugar
Chris Angelico
rosuav at gmail.com
Thu Jun 14 02:37:02 EDT 2018
On Thu, Jun 14, 2018 at 4:12 PM, Brendan Barnwell <brenbarn at brenbarn.net> wrote:
> On 2018-06-13 22:29, Steven D'Aprano wrote:
>>
>> 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?
>
>
> str.re can be a descriptor object which "knows" which string
> instance it is bound to. This kind of thing is common in many libraries.
> Pandas for example has all kinds of things like df.loc[1:3],
> df.column.str.startswith('blah'), etc. The "loc" and "str" attributes give
> objects which are bound (in the sense that bound methods are bound) to the
> objects on which they are accessed, so when you use these attributes to do
> things, the effect takes account of on the "root" object on which you
> accessed the attribute.
>
> Personally I think this is a great way to reduce namespace clutter
> and group related functionality without having to worry about using up all
> the short or "good" names at the top level. I'm not sure I agree with the
> specific proposal here for allowing regex operations on strings, but if we
> do do it, this would be a good way to do it.
>
How is this materially different from:
"some string".re_match(...)
? It's not a grouped namespace in any technical sense, but to any
human, a set of methods that start with a clear prefix is functionally
a group.
That said, though, I don't think any of them need to be methods. The
're' module is there to be imported.
ChrisA
More information about the Python-ideas
mailing list