我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用collections.abc.KeysView()。
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 keys(self): return abc.KeysView(self)
def test_keys(setup_teardown_file): """.keys provides a key view.""" f = setup_teardown_file[3] grp = f.create_group("test") grp.create_group("a") grp.create_group("b") grp.create_group("c") grp.create_group("d") assert isinstance(grp.keys(), KeysView) assert sorted(list(grp.keys())) == ["a", "b", "c", "d"]