【问题标题】:Where is the source code of chain in python? [duplicate]python中chain的源代码在哪里? [复制]
【发布时间】:2017-07-18 20:15:00
【问题描述】:

我想在 python itertools 中查看链的源代码,但这是我在源代码中找到的?为什么他们都是'通过'?

class chain(object):
    """
    chain(*iterables) --> chain object

    Return a chain object whose .__next__() method returns elements from the
    first iterable until it is exhausted, then elements from the next
    iterable, until all of the iterables are exhausted.
    """
    @classmethod
    def from_iterable(cls, iterable): # real signature unknown; restored from __doc__
        """
        chain.from_iterable(iterable) --> chain object

        Alternate chain() contructor taking a single iterable argument
        that evaluates lazily.
        """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __init__(self, *iterables): # real signature unknown; restored from __doc__
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

【问题讨论】:

    标签: python itertools


    【解决方案1】:

    您看到的类可能是另一个模块。 Itertools 和许多其他内置函数是用编译后的 c 编写的。你可以在这里阅读 cpython 代码 https://github.com/python/cpython/blob/3.6/Modules/itertoolsmodule.c#L1792

    在 itertools 文档中,它指出链函数大致相当于:

     def chain(*iterables):
        # chain('ABC', 'DEF') --> A B C D E F
        for it in iterables:
            for element in it:
                yield element
    

    【讨论】:

    猜你喜欢
    • 2021-08-14
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多