Message287102
+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. |
|
Date |
User |
Action |
Args |
2017-02-06 08:31:21 | rhettinger | set | recipients:
+ rhettinger, eric.smith, mrabarnett, THRlWiTi, serhiy.storchaka |
2017-02-06 08:31:21 | rhettinger | set | messageid: <1486369881.34.0.827210061637.issue29459@psf.upfronthosting.co.za> |
2017-02-06 08:31:21 | rhettinger | link | issue29459 messages |
2017-02-06 08:31:20 | rhettinger | create | |
|