[Tutor] Accessing variables in main from functions in a module

spir denis.spir at free.fr
Sat Oct 31 07:32:55 CET 2009

Le Fri, 30 Oct 2009 18:39:42 -0400,
Robert Lummis <robert.lummis at gmail.com> s'exprima ainsi:

> I want to move some functions from my "main" program file to a module
> file because the main file is getting too big for convenience. The
> functions access arrays (lists of lists) that are defined and
> initialised in the main file. How do I reference the main file arrays
> from statements within the module file?

It seems to me you're inverting the logic of your program. Actually, the grid represents the game state (right?) and is /input/ for the game logic. So what is to be exported, if you like, is the grid.
So, you would have for instance a "game_state.py" module defining eg a "grid" object imported into main program.
While maybe the funcs you call "housekeeping" actually hold core logic of the system. If not, it they are really utilities, then have a "housekeeping.py" module that also imports the grid, and itself imported by the main program.

But I guess this would not be the simplest solution. Better make your funcs real funcs (in a sense close to math funcs) by feeding them with all their input as parameters and letting them return their result, as Alan suggests (I'm fan of this point of view, too). Instead of having them wildly read and/or write things here and there.
Or make your game state an object that holds its own modifying methods as suggested by Kent. Take the opportunity to learn this way of designing an application if you don't know it (well). It would certainly be great in your case; even more if the game is logically structured enough for you to easily split its global state into consistent smaller objects (representing "elements" of the game).

la vita e estrany

More information about the Tutor mailing list