iter()无法与datetime.now()一起使用 [英] iter() not working with datetime.now()

查看:92
本文介绍了iter()无法与datetime.now()一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.6.1中的一个简单代码段:

A simple snippet in Python 3.6.1:

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

返回:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

而不是打印出经典的 now()与每个 next()的行为。

instead of printing out the classic now() behavior with each next().

我已经看到类似的代码在Python中工作3.3,我在3.6.1版本中是否缺少某些东西或有什么更改?

I've seen similar code working in Python 3.3, am I missing something or has something changed in version 3.6.1?

推荐答案

这肯定是Python 3.6中引入的错误.0b1。 iter()实现最近切换为使用 _PyObject_FastCall()(优化,请参见问题27128 ),并且必须是此调用才能打破此问题。

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.

由Argument Clinic解析支持的其他C classmethod 方法遇到相同的问题:

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 "<stdin>", line 1, in <module>
StopIteration

如果需要解决方法,则将可调用项包装在<$ c中$ c> functools.partial()对象:

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)

我提交了问题30524- iter(classmethod,sentinel)对于Argument Clinic类方法无效吗? 与Python项目。此修补程序已着陆,并且是3.6.2rc1的一部分。

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.

这篇关于iter()无法与datetime.now()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆