我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用collections.abc.ItemsView()。
def test_MutableMapping_subclass(self): # Test issue 9214 mymap = UserDict() mymap['red'] = 5 self.assertIsInstance(mymap.keys(), Set) self.assertIsInstance(mymap.keys(), KeysView) self.assertIsInstance(mymap.items(), Set) self.assertIsInstance(mymap.items(), ItemsView) mymap = UserDict() mymap['red'] = 5 z = mymap.keys() | {'orange'} self.assertIsInstance(z, set) list(z) mymap['blue'] = 7 # Shouldn't affect 'z' self.assertEqual(sorted(z), ['orange', 'red']) mymap = UserDict() mymap['red'] = 5 z = mymap.items() | {('orange', 3)} self.assertIsInstance(z, set) list(z) mymap['blue'] = 7 # Shouldn't affect 'z' self.assertEqual(sorted(z), [('orange', 3), ('red', 5)])
def test_items(setup_teardown_file): """.items provides an item view.""" f = setup_teardown_file[3] grp = f.create_group("test") grpa = grp.create_group("a") grpb = grp.create_group("b") grpc = grp.create_group("c") grpd = grp.create_group("d") groups = [grpa, grpb, grpc, grpd] names = ["a", "b", "c", "d"] assert isinstance(grp.items(), ItemsView) for i, (key, value) in enumerate(grp.items()): assert key == names[i] assert value == groups[i] # Feature: You can iterate over group members via "for x in y", etc.
def items(self): return abc.ItemsView(self)