[Tutor] Simple Python Program
bgailer at gmail.com
Sun Aug 1 05:11:41 CEST 2010
On 7/31/2010 8:24 PM, Steven D'Aprano wrote:
> On Sun, 1 Aug 2010 04:35:03 am bob gailer wrote:
>> Continue to avoid writing functions. They are not necessary for such
>> a simple program.
> That is *terrible* advice. Absolutely awful.
Well I disagree. I was trying to steer the OP to get the simplest
possible program running, then do incremental expansion.
In my understanding user defined functions serve these purposes:
1 - modularizing large programs
2 - factoring out common code
3 - providing callbacks
4 - choosing callable objects from some collection.
5 - providing a main function for the if __name__ == "__main__" idiom
6 - what else can we think of?
None of these are needed in this simple case.
> Functions should not be avoided unless necessary. Functions should be
> used unless there is a compelling reason to avoid them.
Once the OP has succeeded in getting some program running he will
experience some relief and satisfaction. Then I think it is time to add
language features. But not just for the sake of using them.
> This is not 1972 any more, and we're not teaching kids to write
> spaghetti code with BASIC and GOTO.
Functions have no relationship to avoiding spaghetti code. Python makes
spaghetti code impossible since it lacks a goto or equivalent statement.
> Functions, or their object-oriented
> equivalent methods, are *the* single most important feature of
> programming. And they're simple too
Well - almost simple. Until you get into issues like global vs local
names, positional vs keyword arguments, required vs optional arguments,
default values (especially mutable objects), * and **, generator
functions, magic methods, nested defs, decorators, ....
> -- they might not be the first thing you teach an absolute newbie who has never programmed before
That is what our OP appears to be.
Chapel Hill NC
More information about the Tutor