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.

Author rhettinger
Recipients THRlWiTi, eric.smith, mrabarnett, rhettinger, serhiy.storchaka
Date 2017-02-06.08:31:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1486369881.34.0.827210061637.issue29459@psf.upfronthosting.co.za>
In-reply-to
Content
+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
2017-02-06 08:31:21rhettingersetrecipients: + rhettinger, eric.smith, mrabarnett, THRlWiTi, serhiy.storchaka
2017-02-06 08:31:21rhettingersetmessageid: <1486369881.34.0.827210061637.issue29459@psf.upfronthosting.co.za>
2017-02-06 08:31:21rhettingerlinkissue29459 messages
2017-02-06 08:31:20rhettingercreate