[Tutor] Re: Functions Calling Functions

Andrei project5 at redrival.net
Fri Feb 25 21:11:42 CET 2005


Luke Jordan wrote on Fri, 25 Feb 2005 11:04:11 -0800:

Hi Luke,

> I'm working on a command-line game. Is there anything wrong with
> having each 'chapter' of the game be a function that links to other
> chapters by calling them? I only ask because when a recent traceback
> returned about 40 lines worth of error message, I realized that the
> functions are all being run 'within each other' (ah-ha!).

For a quick'n'dirty prototype while learning Python, I'd say it's
acceptable - although it's likely to end up costing you more time than if
you took a more thought-out approach. If it's something that you intend to
develop, distribute and maintain, I think it's not a good idea at all. It
would be better to find the functionality that your game offers, split it
into separate functions and call those functions using data form a
dictionary/list/database. 

E.g. let's say you have a game with three rooms arranged in a triangle. In
each room the user can play some mini-game or go to one of the connected
rooms. Having functions call each other could end up in the program
crashing if the user just walks from room to room, because you've in effect
got a recursive function going on, basically doing this:

  >>> def a(): c()
  >>> def c(): a()
  a()
  # huge traceback here

Instead, you could have the data in a format like this:

rooms = {
         1: ['First room', guess, 2, 3],
         2: ['Second room', hangman, 3, 1],
         3: ['Third room', safecracker, 1, 2],
         }

The keys in the dictionary are the room numbers. The properties of each
room are described in the form of a list, giving a room name, a mini-game
(which is the name of a function implementing the actual mini-game), the
number of the room to the left and the number of the room to the right.

Now all you'd need is to impelement a "while True" main loop which would
call a function DiplayRoom using one of the room-lists as argument.
DisplayRoom would display the name of the room and offer the user the
choice to play the mini-game or leave to the left or to the right.
Once the user chooses to leave, the new room number is returned as result
and the main loop calls DisplayRoom again, but this time using the
properties list of the new room. 

This approach offers the following advantages:
- you'll never be more than three functions deep or so (depending on the
precise implementation), so it's impossible for the user to crash your
program just by walking around.
- it's very easy to extend the program with new rooms or re-arrange the
'map', since you just have to modify the data. Modifying data is safer and
easier than modifying code.
- it's easier to debug than when you have a billion lines of call stack.
- I expect it to consume less memory

-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.



More information about the Tutor mailing list