Eric V. Smith added the comment: I've been playing around with this. My implementation is basically the naive:
def __getitem__(self, value): return self.group(value) I have the following tests passing: def test_match_getitem(self): pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?') m = pat.match('a') self.assertEqual(m['a1'], 'a') self.assertEqual(m['b2'], None) self.assertEqual(m['c3'], None) self.assertEqual(m[0], 'a') self.assertEqual(m[1], 'a') self.assertEqual(m[2], None) self.assertEqual(m[3], None) with self.assertRaises(IndexError): m['X'] m = pat.match('ac') self.assertEqual(m['a1'], 'a') self.assertEqual(m['b2'], None) self.assertEqual(m['c3'], 'c') self.assertEqual(m[0], 'ac') self.assertEqual(m[1], 'a') self.assertEqual(m[2], None) self.assertEqual(m[3], 'c') with self.assertRaises(IndexError): m['X'] # Cannot assign. with self.assertRaises(TypeError): m[0] = 1 # No len(). self.assertRaises(TypeError, len, m) But because I'm just calling group(), you'll notice a few oddities. Namely: - indexing with integers works, as would group(i). See the discussion of omitting len(). - for defined but missing group names, you get None instead of KeyError - for invalid group names, you get IndexError instead of KeyError I can't decide if these are good (because they're consistent with group()), or bad (because they're surprising. I'm interested in your opinions. I'll attach the patch when I'm at a better computer. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue24454> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com