V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
1989922yan
V2EX  ›  Python

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

  •  
  •   1989922yan · 2014-08-16 12:36:25 +08:00 · 2813 次点击
    这是一个创建于 3781 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> 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))
    第 1 条附言  ·  2014-08-16 14:12:44 +08:00
    ALL code:

    >>> 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))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
    'Make a new Point object from a sequence or iterable'
    result = new(cls, iterable)
    if len(result) != 2:
    raise TypeError('Expected 2 arguments, got %d' % len(result))
    return result

    def __repr__(self):
    'Return a nicely formatted representation string'
    return 'Point(x=%r, y=%r)' % self

    def _asdict(self):
    'Return a new OrderedDict which maps field names to their values'
    return OrderedDict(zip(self._fields, self))

    def _replace(_self, **kwds):
    'Return a new Point object replacing specified fields with new values'
    result = _self._make(map(kwds.pop, ('x', 'y'), _self))
    if kwds:
    raise ValueError('Got unexpected field names: %r' % kwds.keys())
    return result

    def __getnewargs__(self):
    'Return self as a plain tuple. Used by copy and pickle.'
    return tuple(self)

    __dict__ = _property(_asdict)

    def __getstate__(self):
    'Exclude the OrderedDict from pickling'
    pass

    x = _property(_itemgetter(0), doc='Alias for field number 0')

    y = _property(_itemgetter(1), doc='Alias for field number 1')

    ————————————————————

    连接:https://docs.python.org/2/library/collections.html?highlight=collection#namedtuple-factory-function-for-tuples-with-named-fields
    6 条回复    2014-08-17 11:08:37 +08:00
    rcmerci
        1
    rcmerci  
       2014-08-16 12:47:39 +08:00   ❤️ 1
    到处找找吧
    1989922yan
        2
    1989922yan  
    OP
       2014-08-16 14:13:14 +08:00
    @rcmerci

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

    来自 Lib/collections.py 234行的 _class_template,在348行填好参数打印出来
    ruoyu0088
        5
    ruoyu0088  
       2014-08-16 16:05:06 +08:00   ❤️ 1
    _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
        6
    1989922yan  
    OP
       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`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3472 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 00:52 · PVG 08:52 · LAX 16:52 · JFK 19:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.