collections.namedtuple
我阅读了今天的官方文档,并_tuple
在__new__
方法中找到了。我没有找到_tuple
定义的位置。
您可以尝试在 Python 中运行以下代码,它不会引发任何错误。
>>> Point = namedtuple('Point', ['x', 'y'], verbose=True)
class Point(tuple):
'Point(x, y)'
__slots__ = ()
_fields = ('x', 'y')
def __new__(_cls, x, y):
'Create a new instance of Point(x, y)'
return _tuple.__new__(_cls, (x, y)) # Here. Why _tuple?
更新:有什么优点
from builtins import property as _property, tuple as _tuple
这只是为了让tuple
成为受保护的价值吗?我对吗?