copy from <Learn pyton, 3rd edition>, P506
Namespaces: The Whole Story
Now that we’ve examined class and instance objects, the Python namespace story is
complete. For reference, I’ll quickly summarize all the rules used to resolve names
here. The first things you need to remember are that qualified and unqualified names
are treated differently, and that some scopes serve to initialize object namespaces:
• Unqualified names (e.g., X) deal with scopes.
• Qualified attribute names (e.g., object.X) use object namespaces.
• Some scopes initialize object namespaces (for modules and classes).
Simple Names: Global Unless Assigned
Unqualified simple names follow the LEGB lexical scoping rule outlined for functions
in Chapter 16:
Assignment (X= value)
Makes names local: creates or changes the name X in the current local scope,
unless declared global.
Reference (X)
Looks for the name X in the current local scope, then any and all enclosing functions,
then the current global scope, then the built-in scope.
Attribute Names: Object Namespaces
Qualified attribute names refer to attributes of specific objects, and obey the rules for
modules and classes. For class and instance objects, the reference rules are augmented
to include the inheritance search procedure:
Assignment (object.X= value)
Creates or alters the attribute name X in the namespace of the object being qualified,
and none other. Inheritance-tree climbing happens only on attribute
reference, not on attribute assignment.
Reference (object.X)
For class-based objects, searches for the attribute name X in object, then in all
accessible classes above it, using the inheritance search procedure. For nonclass
objects such as modules, fetches X from object directly.
分享到:
相关推荐
资源分类:Python库 所属语言:Python 资源全名:pkgutil_resolve_name-0.0.0a0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
def resolve_name(parent, info): return "Alice" def resolve_age(parent, info): return 30 ``` 5. **执行查询**: 使用`schema.execute`方法执行GraphQL查询。 ```python query = '{ person { name, age ...
pv.Name = 'InputStream' pv.Value = istream document = desktop.loadComponentFromURL('private:stream/swriter', '_blank', 0, (pv,)) text = document.getText() return text ``` #### 读取DOC文档中的...
PEP 3155: Qualified name for classes and functions PEP 412: Key-Sharing Dictionary PEP 362: Function Signature Object PEP 421: Adding sys.implementation SimpleNamespace Using importlib as the ...
在IT行业中,网络编程是不可或缺的一部分,而DNS(Domain Name System)系统作为互联网的重要基础设施,负责将人类可读的域名转换为IP地址。`checkdns`是一个Python编写的实用工具,用于执行DNS相关的查询和检查,...
6. **pathlib模块增强**:Python标准库中的`pathlib`模块在3.7中得到了增强,增加了更多方便操作路径的方法,如`relative_to`和`resolve`,使得文件路径操作更加直观和面向对象。 7. **f-string**:3.7版本引入了f-...
results = c.name.resolve('/ipfs/Qm...') # 使用文件哈希查询 print(results) ``` **应用实例** IPFS在许多领域都有潜在的应用,例如: 1. **分布式存储**:存储敏感数据,如医疗记录或个人隐私信息,以增强...
在文件处理上,Python 3.8引入了`pathlib`模块的增强,如`Path.resolve()`方法现在能更有效地处理符号链接,而`Path.glob()`和`Path.rglob()`增加了对递归模式的支持。 对于开发者工具,Python 3.8的`pdb`调试器...
Python的`pydns`库是Python编程语言中用于DNS(Domain Name System)解析的重要工具,它为开发者提供了方便的接口来处理DNS查询和记录管理。`pydns`库的最新版本,据描述是从国外获取,且兼容Python 2.7、3及4,这...
def resolve_hello(self, info, name): return f"Hello, {name}!" app.add_url_rule( "/graphql", view_func=GraphQLView.as_view("graphql", schema=Query, graphiql=True), ) if __name__ == "__main__": ...
DNS(Domain Name System)是互联网上的一个关键服务,它负责将人类可读的域名转换为IP地址,以便计算机可以理解。在Python中,通常使用`socket`库进行DNS解析,但这种方式通常是同步的,可能会阻塞程序的执行,直到...
resolve_after_mins=30 ) create_alert("myProject", 80) ``` 在这个例子中,我们创建了一个名为`create_alert`的函数,它接受项目名称和阈值作为参数,并创建一个在CPU使用率超过给定阈值时触发的Wavefront警报...
if __name__ == "__main__": asyncio.run(main()) ``` 在这个例子中,我们定义了一个`Query`类型,包含一个`hello`字段,然后创建了一个GraphQL schema。`http_server`函数用于启动一个HTTP服务器,监听8080端口,...
你可以通过`name.publish()`发布IPFS对象,并用`name.resolve()`解析IPNS名称。 5. **DAG(有向无环图)操作**:IPFS使用DAG数据结构存储数据,"aioipfs"提供了对DAG的操作,如创建、添加和遍历。 6. **命令行接口...
print(f"Cannot resolve {arg}: Unknown host") ``` **代码解释**: 1. 使用 `urllib.request` 库来发送 HTTP 请求。 2. 使用 `json` 库来解析返回的 JSON 数据。 3. 支持输入单个 IP 地址、域名或包含多个 IP ...
在互联网通信过程中,DNS(Domain Name System,域名系统)扮演着极其重要的角色,它负责将易于记忆的域名转换为计算机能够理解的 IP 地址。Python 提供了多种方式来实现 DNS 查询功能,其中最简便的方法是通过内置...
在Python编程中,处理超时异常是网络编程和多线程应用中常见的需求。超时异常处理可以提高程序的健壮性,防止因为外部调用或网络请求耗时过长而导致整个程序挂起。本文将介绍如何在Python中自定义超时异常的几种方法...
result = handle.submit("local", "var", "resolve string {STAF/Config/OS/Name}") if result.rc != 0: print("Error submitting request, RC:%d, Result:%s" % (result.rc, result.result)) else: print("OS ...
例如,如果字符串中有 `${var_name}` 这样的全局变量表达式,该函数会查找 `global_var_dic` 字典中对应的键为 `var_name` 的值,并替换掉表达式。这个功能允许动态构建请求参数。 4. **错误处理**:在解析全局变量...