这是语义问题,我正在为我的实验室一起破解的模块编写教程,我想知道是否有使用点表示法访问类属性的正确术语。例如:
class funzo:
def __init__(self,string):
self.attribute = string
fun = funzo("Sweet sweet data")
fun.attribute
Now one can access the string by ??? the object named fun.
我一直在使用术语“点引用”,但这似乎是错误的。我四处搜索并检查了 python 词汇表,但我似乎找不到任何东西。
最佳答案
属性访问,这来自 customizing it 上的语言引用部分:
The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of
x.name
) for class instances.
(强调我的)
所以你的报价可以更好地措辞为:
Now, one can access the attribute
attribute
by using dotted expressions on the instance namedfun
.
从 Python Glossary 的“属性”条目中找到术语点式表达式 .
文档中作为点表达式的替代方法的另一种选择是,如您的标题所示,点符号。这可以在 Standard Type Hierarchy 的 Callables 部分找到。 ,因此您可以考虑的另一个选择是:
Now, one can access the attribute
attribute
using dot-notation on the instance namedfun
.
在我看来,如果有人至少有一些编程语言经验,那么两者都是完全可以理解的。我更喜欢术语点符号,因此,会使用后者。
关于python - 在 python 中,使用点表示法访问类属性的正确命名法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39536013/