Python文档的状态为https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy:
下面的某些类型描述中包含一个列出了“特殊属性”的段落。这些属性是提供对实现的访问权限,并不旨在用于一般用途。它们的定义将来可能会更改。
因此,对我来说,不鼓励使用特殊属性。
是真的吗?
如果是这样:是否存在一种“非特殊”方式来访问每个特殊属性(例如len
的内置函数__len__
)?
仅说明一下,我在说什么:
对于用户定义的功能,特殊属性是:
仅是magic methods and variables
,您可以使用它们的名称(如果它们在对象中)进行访问。
示例
class Test:
def __init__(self, name, value):
self.name = name
self.value = value
obj = Test('test name', 'with some value')
print(obj.__dict__)
输出
# > python test123.py
{'name': 'test name', 'value': 'with some value'}