Issue 20496: function definition tutorial encourages bad practice (original) (raw)

Issue20496

Created on 2014-02-03 05:09 by aisaac, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg210077 - (view) Author: Alan Isaac (aisaac) Date: 2014-02-03 05:09
Section 4.6 of the tutorial introduces function definition: http://docs.python.org/3/tutorial/controlflow.html#defining-functions The first example defines a function that *prints* a Fibonacci series. A basic mistake made by students new to programming is to use a function to print values rather than to return them. In this sense, the example encourages bad practice and misses an opportunity to instruct. Since they have already met lists in Section 3, I suggest that returning a list of the values and then printing the list would enhance the tutorial.
msg210657 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-02-08 15:00
There is a later example introduced by "It is simple to write a function that returns a list of the numbers of the Fibonacci series, instead of printing it:" that does exactly what you suggest.
msg266796 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-06-01 03:17
I agree with Terry. The print example is also useful to show the implicit return value of fib(). Quoting from the same section: [...] In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None [...]
History
Date User Action Args
2022-04-11 14:57:58 admin set github: 64695
2016-06-01 03:17:27 berker.peksag set status: open -> closednosy: + berker.peksagmessages: + resolution: not a bugstage: resolved
2014-02-08 15:00:17 terry.reedy set nosy: + terry.reedymessages: +
2014-02-03 05:09:47 aisaac create