gpt4 book ai didi

python - "unqualified on right hand side"在 OOP(Python)中意味着什么?

转载 作者:行者123 更新时间:2023-12-01 04:08:30 26 4
gpt4 key购买 nike

我在阅读 python 中的 oops 概念时遇到了“右手边不合格”短语,例如 self._customer = customer。这句话试图解释什么?完整的声明是

For example, the command, self._customer = customer, assigns the instance variable self._customer to the parameter customer; note that because customer is unqualified on the right-hand side, it refers to the parameter in the local namespace. --Data Structures and Algorithms in Python p. 72

最佳答案

根据Python docs

qualified name

A dotted name showing the “path” from a module’s global scope to a class, function or method defined in that module, as defined in PEP 3155. For top-level functions and classes, the qualified name is the same as the object’s name:

...

When used to refer to modules, the fully qualified name means the entire dotted path to the module, including any parent packages, e.g. email.mime.text:

更简单地说,在 Python 中限定名称意味着您显式定义其范围。因此,self._customer 是一个限定名称(它标识封闭类的实例变量customer),而裸露的customer引用则指定任何范围限定。

当名称不合格时,Python 应用词法作用域规则来尝试查找变量,并进行搜索(按顺序)

  • 局部变量(包括函数参数)
  • 如果我们处理嵌套函数定义,则为任何外部函数的本地变量
  • 全局变量
  • 内置变量

关于python - "unqualified on right hand side"在 OOP(Python)中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35276738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com