[Tutor] >> Bananas << A -bunch- of replying.

Chris McCormick cmccormick@thestate.com
Fri, 09 Mar 2001 11:37:24 -0500


D-Man,
    Thanks for your reply.

<snipped>

|    As usual, I have a piggyback question:
|
|    I'm always trying to pass information from my main module, game.py,
to
|    functions contained in other modules. Those functions need to
update
|    the information so that it can be passed again on the next loop.
The
|    problem I have is making the information available to everyone. The

|    best solution I have come up with so far is to have a "globals"
|    dictionary, and pass it as an argument to the functions, like so:

I would recommend using a class.  A class is essentially some
functions bundled together with some data they operate on.  For
example:

######## module Banana.py

class Banana :
    def __init__( self ) :
        self.current_age = 0 # it start out new right?
        self.ripeness = 0
        self.is_bad = 0      # I miss the keyword 'false'
        self.is_eaten = 0

    def mature( self ) :
        self.ripeness += 1
        # the += operator only works in >= 2.0
        # otherwise use
        ## self.ripeness = self.ripeness + 1

*** I am using a banana class, from my flora.py module.  A dictionary
all the instances of the Banana class.  I have a Banana.mature function,
too. :-) ***

You should be able to get rid of that 'globals' dictionary.  You
commented it as "contain needed info".  That's exactly what classes are
for.

If you want the count of bananas, use len( bananas_dict.keys() ).

***  I automatically name each instance of my banana class, so that I
can retrieve it from the dictionary.  I name them banana0, banana1,
banana2, and so on.  My dictionary looks something like this:

{'banana1':<class instance...>, 'banana2':<class instance...>}

The problem lies in the function to generate unique names.  It has to
get the initial slate of names as it generates each instance and puts it
in the dictionary:

bananacount = input('How many bananas do you wanna see? ')  ## Get
number of bananas
gameGlobals['banana_count'] = bananacount
for x in range(0,
bananacount):                                                   ##
Create bananas dictionary
    banana_name = "banana" + str(x)
<snip>
    bananas[banana_name] = new_banana

So far, so good, right?  But when new bananas are propagated later, I
can't just use  len( bananas_dict.keys() ), because some of the bananas
have gone away.  If I have bananas 1-20, the last one is named
banana19.  Let's say banana2 dies or gets eaten.  Now, my dictionary has
19 items.  If I take the length and generate a name, I get banana19
again.  So, what I've done is to create a dictionary that can hold a
banana_count item.  Then I update it when a banana goes through its
die() function.  The problem is making that data available all around
the program, in different modules.

|    Is there *any* way to make a dictionary or a list global to *all*
|    modules, or to make it importable? On a general level, what's the
best

It is importable.  In  window.py put

import game


then use

game.globals

to access it.

*** Now, this makes things much easier.  I can put all global data,
along with functions to change them, in a globals module. ***

<snip>

No problem.  If you don't know or understand classes/OOP feel free to
ask and someone (maybe even me ;-)) will come up with a good
introduction with examples, etc.

-D

*** thanks.  I'll keep working on it. ***

- Chris