While studying about Generator comprehension, I learned that they encloseds in (). Like:
G = (x**2 for x in range(4))
This will generate a Generator object which can either be used with G.__next__()
or list(G)
On further reading, I found that other methods/functions that uses () are also generators. Example:
''.join(x.upper() for x in 'aaa,bbb,ccc'.split(','))
a,b,c = (x + '\n' for x in 'aaa,bbb'ccc'.split(','))
sum(x**2 for x in range(4))
etc..
before knowing about Generators, I used to think about these methods/functions to be 'normal methods'. I mean it seemed to me that they normally take arguments, process them and return. But now, they are referred to be 'Generator expressions' or whatever. So, does that mean that any expression that is enclosed in () is a generator expression? Does this make range() also a generator (I know it is not)? If not, then how can we distinguish?