[疑问] _tuple 是哪里来的变量呢??

2014-08-16 12:36:25 +08:00
 1989922yan
>>> 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))
2757 次点击
所在节点    Python
6 条回复
rcmerci
2014-08-16 12:47:39 +08:00
到处找找吧
1989922yan
2014-08-16 14:13:14 +08:00
@rcmerci

真没有,真搜过了
arbipher
2014-08-16 14:21:33 +08:00
看下namedtuple源代码试试?
vmebeh
2014-08-16 15:13:05 +08:00
这是 Point = namedtuple('Point', ['x', 'y'], verbose=True) 回显的

来自 Lib/collections.py 234行的 _class_template,在348行填好参数打印出来
ruoyu0088
2014-08-16 16:05:06 +08:00
_tuple is tuple, you can input following code to verify it:

from collections import namedtuple
Point = namedtuple("Point", "x, y", verbose=True)
print Point.__new__.func_globals["_tuple"] is tuple
1989922yan
2014-08-17 11:08:37 +08:00
@ruoyu0088

yep,

in the source code, where it write `from builtins import property as _property, tuple as _tuple`

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/128215

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX