top | item 39495792

(no title)

igsomething | 2 years 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

discuss

order

No comments yet.