Raymond Hettinger added the comment:
+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.
----------
nosy: +rhettinger
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue29459>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com