21 ms·
They are different. def square_vals(x : list): return (val * val for val in x) is a function that takes a list and returns a generator. While
by igsomething 3y ago
They are different.
def square_vals(x : list):
return (val * val for val in x)
is a function that takes a list and returns a generator. While
def square_vals(x : list):
for val in x:
yield val * val
is a generator itself.
The first case creates an anonymous generator. When in doubt you can always use the dis module: https://docs.python.org/3/library/dis.html https://docs.python.org/3/library/dis.html