2017-09-23 117 views
0

WERKZEUG v0.11 我stduy WERKZEUG的源代碼,文件wsgi.py類ClosingIterator,按功能implements_iterator decorted:裝飾器「implements_iterator」的功能是什麼?

wsgi.py

@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:

implements_iterator = _identity 

_identity = lambda x: x 

的問題是: 是什麼implements_iterator的功能?

回答

2

WERKZEUG目標兩者的Python 2和Python 3,如果您在compat.py向上滾動,你可以看到implements_iterator是爲Python 2定義如下:

def implements_iterator(cls): 
    cls.next = cls.__next__ 
    del cls.__next__ 
    return cls 

這樣一個類來實現一個__next__方法(在Python 2中稱爲next),並且無需任何修改即可用於Python 2和Python 3。