需要了解Python生成器对象 [英] Need to understand Python generator object

查看:76
本文介绍了需要了解Python生成器对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下:

name = 'TODD'
chars = set('AEIOU')
for ii in range(-1, int(math.copysign(len(name) + 1, -1)), -1):
    if any((cc in chars) for cc in name[ii]):
        print 'Found'
    else:
        print 'Not Found'

我知道any(...)内部是一个生成器对象.我不明白的是缺少括号-如果括号属于any()函数,在生成器表达式周围是否应该有另一套括号?

I understand that what's inside any(...) is a generator object. What I don't understand is the lack of parentheses - if the parentheses belong to the any() function, shouldn't there be another set of parentheses around the generator expression?

谢谢.

推荐答案

在仅带有一个参数

The parenthesis can be omitted when used in function calls with only one argument, the generator expression syntax specifically allows for it.

仅具有一个参数的调用可以省略括号.有关详细信息,请参见通话部分.

这篇关于需要了解Python生成器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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