global variables - how??

Greg Ewing greg at cosc.canterbury.ac.nz
Wed Apr 12 22:36:34 EDT 2000


Anders Eggers-Krag wrote:
> 
> in my case I could otherwise send them along with every function, creating a
> huge
> overhead passing a million pointers around

If you find yourself doing that, your program is
badly in need of redesign. Think about grouping
related global variables together and making them
attributes of a class instance rather than globals. 
Then pass a reference to the instance instead of 
passing all the variables separately. Even better, 
make the functions which operate on those variables 
methods of the class whenever you can.

> but this really anoys me, and I *was* starting to like python...

Don't give up! It's always possible to redesign
any program which uses a lot of global variables
so that it uses very few, or even none. And in
my experience, the new design is *always* better -
clearer, easier to understand, easier to modify
and extend.

And once you get over global-variable-withdrawal,
you'll just keep liking Python more and more!

-- 
Greg Ewing, Computer Science Dept,
+--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+



More information about the Python-list mailing list