You need to understand the Python generator object

In the following:

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'

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

Thank.

+5
source share
4 answers

The brackets can be omitted when using functions with only one argument in calls; the syntax of the generator expression allows this specially .

Brackets can be omitted in calls with only one argument. See the Calls section for more details .

+12
source

, .

:

.

+2

No, extra paranas are not needed, and in fact, Parens are always needed for the "Boolean expression" you are testing, see these two simple examples:

In [37]: any(i > 10 for i in range(19))
Out[37]: True

In [38]: all(i > 10 for i in range(19))
Out[38]: False

You have a function call with one argument (an expression for your generator), so there is no need for parsing. See the docs expression expressions for more information .

+2
source

For function calls with one argument (this is a generator expression) they are not required for docs

Brackets can be omitted in calls with only one argument. See the Challenges Section.

+1
source

All Articles