how to distinguish return from print()

Meredith Montgomery mmontgomery at levado.to
Wed May 25 11:42:08 EDT 2022


ram at zedat.fu-berlin.de (Stefan Ram) writes:

> Meredith Montgomery <mmontgomery at levado.to> writes:
> ...
>>def f(x):
>>    return x + 1
> ...
>>>>> print("hello")
>
>   To me, what would make more sense would be:
>
>   Teacher:
>
> |>>> def r():
> |...     return 7
> |...
> |>>> def p():
> |...     print( 7 )
> |...
> |>>> r()
> |7
> |>>> p()
> |7
>
>   Pupil:
>
>   That was a very instructive example, teacher. Thank you!
>   But could you also show us some context where the calls 
>   to p and r show some difference?
>
>   Teacher:
>   
> |>>> print( r() )
> |7
> |>>> print( p() )
> |7
> |None
>
> |>>> x = r()
> |>>> x = p()
> |7
>
>   Pupil:
>
>   Now I'm confused. What's "None"?
>
>   Teacher:
>
>   ...

I did do this too.  I think that was helpful.  I told them --- a print
doesn't let you easily ``capture'' a value calculated in a procedure.
Thank you!


More information about the Python-list mailing list