[Tutor] scoping oddity

Alan Gauld alan.gauld at freenet.co.uk
Mon May 9 22:45:29 CEST 2005


> Obviously, you can't put every variable on the 'def' line.

Obviously. Or at least maybe not. If your program is well designed
its unusual to have more that a few global varoables so you could
in fact pass them in to each fuinctrion - but thats bad practice too.

You shouldn't ever have to. If you access every variable in every
function (or even in one function!) then there is something
fundamentally wrong with your design.  In an ideal world:

Functions should be designed to be highly cohesive - meaning all the
things inside are related to each other - and loosely coupled -
meaning
they don't rely on a lot of data being passed between them.

Functions should also have few side-effcts - they shouldn't alter
global variables - and should be predictable and stateless - meaning
calling the same function with the same arguments should return the
same results each time.

Following these rules should result in code that is more reliable,
more maintainable and easier to reuse.

> utility of some sort that will present me with a list of the
variables used
> in an application?

dir()

will tell you what names exist in the namespace of the argument.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list