This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `__contains__` and `get` methods for match objects?
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: THRlWiTi, eric.smith, mrabarnett, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-02-06 08:00 by THRlWiTi, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg287096 - (view) Author: Thrlwiti (THRlWiTi) * Date: 2017-02-06 08:00
__getitem__ was added to match objects as a resolution of issue24454.

Wouldn't it be nice to also have `__contains__` and `get` methods for match objects? Is it even feasible to implement them in neat way?

They should work similar to dictionaries, i.e:

```
match = re.match('(?P<a>a)', 'a')
match.get('b')  # should return None
match.get(1)  # should return 'a'
'a' in match # True
'b' in match # False
```
msg287101 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-06 08:23
I predicted that that change will open a can of worms.

The match object is not a dictionary. I don't see a need in new methods. The get() method you propose looks virtually the same as the group() method. "'a' in match" is virtually the same as "match.get('a') is not None".
msg287102 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-02-06 08:31
+1 for m.get(key[, default])
-1 for __contains__

The get() makes logical sense and it is perfectly reasonable to want a default value for a missing group.

The contains idea makes less sense and is problematic on several fronts.  "'a' in match" only works if the match object is not None but it looks like a traditional membership test.  Also, I think it will lead to confusion over whether contains is testing the key or the value:

  m = re.match('(?P<a>[ab])', 'a')
  'a' in m    # True
  'b' in m    # False

  m = re.match('(?P<a>[ab])', 'b')
  'a' in m    # True  
  'b' in m    # False

  m = re.match('(?P<a>[ab])', 'c')
  'a' in m    # TypeError
  'b' in m    # TypeError

IMO, it is better to leave out __contains__ and let people just use groupdict() where they know for sure that normal dict semantics apply.
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73645
2017-02-06 08:31:21rhettingersetnosy: + rhettinger
messages: + msg287102
2017-02-06 08:23:06serhiy.storchakasetmessages: + msg287101
2017-02-06 08:00:11THRlWiTicreate