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

D-Man dsh8290@rit.edu
Fri, 9 Mar 2001 15:27:33 -0500


On Fri, Mar 09, 2001 at 11:37:24AM -0500, Chris McCormick wrote:
| D-Man,
|     Thanks for your reply.
| 
| <snipped>
| 
| I would recommend using a class.  A class is essentially some
| functions bundled together with some data they operate on.  For
| example:
| 
| 
| *** 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. :-) ***

Good.  

| 
| 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:
| 

There are a couple of ways around this.  I would recommend storing the banana's
name in the Banana instance.  You can have a class (aka static) member
that keeps track of how many bananas have been created. 

class Banana :
    instance_count = 0  # this is in the class object

    def __init__( self ) :
        Banana.instance_count += 1
        self.name = "banana" + str( Banana.instance_count )


This sort of implementation will allow your function to not know and
not care what the current number is.


I don't know if it would be very helpful, but you could make the
bananas hashable.  This means defining a __hash__ function (or some
similar name, check the docs).  In this case it would suffice for it
to simply

    return hash( self.name )

and let the string object's authors do the hard work of devising a
good hashing function.

| 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

Ok.  First how about adding a space between "banana" and "19" (to make
extraction easier)?

def get_highest_count( banana_dict ) :
    names = banana_dict.keys()
    names.sort()
    highest = 0
    for name in names :
        # the next line will throw ValueError if I messed up the
        # split, or if the name doesn't follow the pattern
        index = int( string.split( name , " " )[1] )
        if index > highest : highest = index
    return highest

but obviously the class technique above is simpler.

| |    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. ***

Yes, if global data is absolutely necessary (in some cases it is) a
module specifically for it is a good idea.

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

No problem.  That's the way to learn it and improve your techniques.

-D