[Tutor] running a script from IDLE

Kirby Urner urnerk@qwest.net
Fri, 08 Mar 2002 09:35:25 -0800


At 07:42 AM 3/8/2002 -0500, Erik Price wrote:

>So instead of global variables, it's good form for
>functions to simply use arguments and act upon those?

One use of global variables that's not controversial
is to fix them to constant values and consult them
in functions, e.g. start at the top by going

PHI = (1 + math.sqrt(5))/2

But that's a constant more than a variable (still
global), the kind of thing many languages handle at
compile time with #DEFINE statements and the like
(using all caps for constants is a holdover from
those languages -- a stylistic flag).

>If this is what you're saying, I can see that, since
>it makes the code more reuseable in scripts that don't
>provide these same [global] variables.

It's actually a sort of deep question how to make
use of globals.  Writing a module and testing the
functions as you add them doesn't actually prevent
weird constructs -- it only makes them less likely.

>Isn't using a globalized varible just a way to save
>time/space by not having to declare it as an argument?
>
>Always curious about good style,
>
>Erik

It's when you start burying changes to shared variables
deep in code that's ostensibly focused on something else,
that your readers (which includes yourself) get lost.

This might mean passing more arguments and returning
altered contents, rather than changing contents as a
side effect.

Giving your functions a kind of standalone integrity,
makes your code more robust overall.

However, there's a tradeoff.  A very generic coding
style can be too abstract for the problem at hand.
A language as easy to use as Python places less
premium on writing these absolutely generic utilities
at every turn -- which has the effect of making the
results look less "academic" (e.g. less like LISP).

Kirby