python ChainMap的管理用法

410次阅读
没有评论

python

说明:

1、ChainMap的主要用例是提供一种有效的方法来管理多个范围或上下文,并处理重复键的访问优先级。

2、当有多个存储重复键的字典访问它们的顺序时,这个功能非常有用。

在ChainMap文档中找到一个经典的例子,它模拟Python如何分析不同命名空间中的变量名称。

当Python搜索名称时,它会依次搜索当地、全局和内置的功能域,直到找到目标名称。Python作用域是将名称映射到对象的字典。

为了模拟Python的内部搜索链,可以使用链映射。

实例

>>> import builtins
 
>>> # Shadow input with a global name
>>> input = 42
 
>>> pylookup = ChainMap(locals(), globals(), vars(builtins))
 
>>> # Retrieve input from the global namespace
>>> pylookup["input"]
42
 
>>> # Remove input from the global namespace
>>> del globals()["input"]
 
>>> # Retrieve input from the builtins namespace
>>> pylookup["input"]
<built-in function input>

以上就是python ChainMap的管理用法,希望对大家有所帮助。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-05-01发表,共计625字。
新手QQ群:570568346,欢迎进群讨论 Python51学习