mirror of
https://github.com/python/cpython.git
synced 2025-07-29 14:15:07 +00:00
#1826: allow dotted attribute paths in operator.attrgetter.
This commit is contained in:
parent
b0b0317ba2
commit
e2065c65d3
4 changed files with 78 additions and 5 deletions
|
@ -386,6 +386,26 @@ class OperatorTestCase(unittest.TestCase):
|
|||
raise SyntaxError
|
||||
self.failUnlessRaises(SyntaxError, operator.attrgetter('foo'), C())
|
||||
|
||||
# recursive gets
|
||||
a = A()
|
||||
a.name = 'arthur'
|
||||
a.child = A()
|
||||
a.child.name = 'thomas'
|
||||
f = operator.attrgetter('child.name')
|
||||
self.assertEqual(f(a), 'thomas')
|
||||
self.assertRaises(AttributeError, f, a.child)
|
||||
f = operator.attrgetter('name', 'child.name')
|
||||
self.assertEqual(f(a), ('arthur', 'thomas'))
|
||||
f = operator.attrgetter('name', 'child.name', 'child.child.name')
|
||||
self.assertRaises(AttributeError, f, a)
|
||||
|
||||
a.child.child = A()
|
||||
a.child.child.name = 'johnson'
|
||||
f = operator.attrgetter('child.child.name')
|
||||
self.assertEqual(f(a), 'johnson')
|
||||
f = operator.attrgetter('name', 'child.name', 'child.child.name')
|
||||
self.assertEqual(f(a), ('arthur', 'thomas', 'johnson'))
|
||||
|
||||
def test_itemgetter(self):
|
||||
a = 'ABCDE'
|
||||
f = operator.itemgetter(2)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue