是否使用 '_'对于namedtuple中的typename有什么特别的吗?

发布于 2024-12-12 05:09:21 字数 203 浏览 0 评论 0原文

我正在查看在 namedtuple 中使用 _ 作为类型名的代码。我想知道这样做的目的是什么。

example = namedtuple('_', ['NameOfClass1', 'NameOfClass2'])

为什么不直接使用String

I'm looking at code that does uses an _ for typename in a namedtuple. I was wondering what the purpose of this is.

example = namedtuple('_', ['NameOfClass1', 'NameOfClass2'])

Why not just use String?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-12-19 05:09:21

这是一个有点奇怪的命名元组的例子。重点是为类及其属性赋予有意义的名称。一些功能(例如 __repr__ 和类文档字符串)的大部分好处都来自于有意义的名称。

FWIW,namedtuple 工厂包含一个详细选项,可以轻松了解工厂如何处理其输入。当 verbose=True 时,工厂打印出它创建的类定义:

>>> from collections import namedtuple
>>> example = namedtuple('_', ['NameOfClass1', 'NameOfClass2'], verbose=True)
class _(tuple):
    '_(NameOfClass1, NameOfClass2)' 

    __slots__ = () 

    _fields = ('NameOfClass1', 'NameOfClass2') 

    def __new__(_cls, NameOfClass1, NameOfClass2):
        'Create new instance of _(NameOfClass1, NameOfClass2)'
        return _tuple.__new__(_cls, (NameOfClass1, NameOfClass2)) 

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new _ 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 '_(NameOfClass1=%r, NameOfClass2=%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 _ object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('NameOfClass1', 'NameOfClass2'), _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) 

    NameOfClass1 = _property(_itemgetter(0), doc='Alias for field number 0')
    NameOfClass2 = _property(_itemgetter(1), doc='Alias for field number 1')

That is a somewhat odd example of a namedtuple. The whole point is to give meaningful names to the class and its attributes. Some of the features such as the __repr__ and the class docstring derive most of their benefit from meaningful names.

FWIW, the namedtuple factory includes a verbose option which makes it easy to understand what the factory is doing with its inputs. When verbose=True, the factory prints out the class definition it has created:

>>> from collections import namedtuple
>>> example = namedtuple('_', ['NameOfClass1', 'NameOfClass2'], verbose=True)
class _(tuple):
    '_(NameOfClass1, NameOfClass2)' 

    __slots__ = () 

    _fields = ('NameOfClass1', 'NameOfClass2') 

    def __new__(_cls, NameOfClass1, NameOfClass2):
        'Create new instance of _(NameOfClass1, NameOfClass2)'
        return _tuple.__new__(_cls, (NameOfClass1, NameOfClass2)) 

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new _ 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 '_(NameOfClass1=%r, NameOfClass2=%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 _ object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('NameOfClass1', 'NameOfClass2'), _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) 

    NameOfClass1 = _property(_itemgetter(0), doc='Alias for field number 0')
    NameOfClass2 = _property(_itemgetter(1), doc='Alias for field number 1')
烟雨扶苏 2024-12-19 05:09:21

只是意味着生成的类的名称是不相关的。

Just means that the name of the generated class is irrelevant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文