首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >装饰师"implements_iterator“的功能是什么?

装饰师"implements_iterator“的功能是什么?
EN

Stack Overflow用户
提问于 2017-09-23 06:36:32
回答 1查看 153关注 0票数 1

Werkzeug v0.11 i使用函数ClosingIterator装饰了wsgi.py文件中的类Werkzeug的源代码:

wsgi.py

代码语言:javascript
复制
@implements_iterator
class ClosingIterator(object):

    """The WSGI specification requires that all middlewares and gateways
    respect the `close` callback of an iterator.  Because it is useful to add
    another close action to a returned iterator and adding a custom iterator
    is a boring task this class can be used for that::

        return ClosingIterator(app(environ, start_response), [cleanup_session,
                                                              cleanup_locals])

    If there is just one close function it can be passed instead of the list.

    A closing iterator is not needed if the application uses response objects
    and finishes the processing if the response is started::

        try:
            return response(environ, start_response)
        finally:
            cleanup_session()
            cleanup_locals()
    """

我在文件implements_iterator中找到了_compat.py的定义:

代码语言:javascript
复制
implements_iterator = _identity

_identity = lambda x: x

问题是: implements_iterator的功能是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-23 06:42:24

Werkzeug的目标是Python2和Python3。如果您在compat.py中向上滚动,您可以看到implements_iterator是为Python2定义的:

代码语言:javascript
复制
def implements_iterator(cls):
    cls.next = cls.__next__
    del cls.__next__
    return cls

这允许类实现__next__方法(在Python2中称为next ),并且不需要任何修改就可以同时处理Python2和3。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46376804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档