Generator comprehension - list None

Ben Finney ben+python at benfinney.id.au
Tue Oct 18 08:01:06 EDT 2016


Sayth Renshaw <flebber.crue at gmail.com> writes:

> This I did however I also get a list of None. I don't understand where
> none comes from. Can you please clarify?

Every function in Python, if it returns, returns a value.

In an expression that consists of only a function call, the expression
resolves to the return value from calling that function.

The ‘print’ function's return value is always None.

> a = (print("Got {0}".format(num[0])) for num in enumerate(range(10)))
> b = list(a)
> print(b)

Try this:

    a = (None for num in enumerate(range(10)))
    b = list(a)
    print(b)
    print(repr(b))

Now try this:

    def always_return_none(dummy):
        return None

    a = (always_return_none("Got {0}".format(num[0])) for num in enumerate(range(10)))
    b = list(a)
    print(b)
    print(repr(b))

Now try this:

    c = print("Foo")
    print(c)
    print(repr(c))

Does that help?

-- 
 \        “I knew it was a shocking thing to say, but … no-one has the |
  `\        right to spend their life without being offended.” —Philip |
_o__)                                              Pullman, 2010-03-28 |
Ben Finney




More information about the Python-list mailing list