iter() not working with datetime.now()

前端 未结 2 694
心在旅途
心在旅途 2021-02-05 00:23

A simple snippet in Python 3.6.1:

import datetime
j = iter(datetime.datetime.now, None)
next(j)

returns:

Traceback (most recent         


        
2条回答
  •  遇见更好的自我
    2021-02-05 00:56

    This is definitely a bug introduced in Python 3.6.0b1. The iter() implementation recently switched to using _PyObject_FastCall() (an optimisation, see issue 27128), and it must be this call that is breaking this.

    The same issue arrises with other C classmethod methods backed by Argument Clinic parsing:

    >>> from asyncio import Task
    >>> Task.all_tasks()
    set()
    >>> next(iter(Task.all_tasks, None))
    Traceback (most recent call last):
      File "", line 1, in 
    StopIteration
    

    If you need a work-around, wrap the callable in a functools.partial() object:

    from functools import partial
    
    j = iter(partial(datetime.datetime.now), None)
    

    I filed issue 30524 -- iter(classmethod, sentinel) broken for Argument Clinic class methods? with the Python project. The fix for this has landed and is part of 3.6.2rc1.

提交回复
热议问题