trouble understanding None

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Wed Nov 12 04:16:52 CET 2003


On Wed, 12 Nov 2003 02:56:43 GMT, Jakle wrote:
> def S():
>     # [print a bunch of stuff]
>
> def T():
>     # [print a bunch of other stuff]
>
> print S(), T()
> *******************************************
> 
> OK, the test prints the letters, but also prints "None" at the end of
> each function.

That's because you've asked for the following sequence of events:

    - Invoke S()...
        - which prints a bunch of stuff
        - then returns None.
    - Print the return value of S().
    - Invoke T()...
        - which prints a bunch of other stuff
        - then returns None.
    - Print the return value of T().

If you just want the functions invoked (called) instead of getting their
return value and printing it, then do that.

Define the function:

    >>> def S():
    ...     print "Bunch of stuff"
    ...

Print the return result of S(), which necessitates calling the function
and doing whatever is inside it:

    >>> print S()
    Bunch of stuff
    None

Call the function, thus doing whatever is inside it, then throw away the
return value:

    >>> S()
    Bunch of stuff


Please try to get into the practice of reducing the problem you want to
describe to a minimal working example.  This often has the side effect
that you understand the problem better, and don't end up needing to
post it.  In the cases where that doesn't happen, at least you've got
something that doesn't have irrelevant extra code in it.

-- 
 \        "If you ever drop your keys into a river of molten lava, let |
  `\              'em go, because, man, they're gone."  -- Jack Handey |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>




More information about the Python-list mailing list