[Tutor] >> BANANAS! << The answer. :-)

Chris McCormick livelikemad@yahoo.com
Fri, 9 Mar 2001 07:19:58 -0800 (PST)


--0-1728907065-984151198=:3915
Content-Type: text/plain; charset=us-ascii

So, can you have a "significant rewrite" of a program that's fewer than 1000 lines?  I got the chance yesterday to play with my virtual world program again, and tried some of the things suggested a couple of days ago.  For those of you late to the conversation, I was trying to do the following:
- Create a list.
- Append instances of a class to the list.
- Remove those instances without regard to their place in the list.
The list happened to contain bananas, which I wanted to remove when they died of old age or got eaten.
The obvious solution, now that I think about it (and mentioned by more than one person) was to grab the bananas by name, which implies the use of a dictionary.  Once I got the hang of how to refer to values inside items inside a dictionary, it was all smooth sailing:
>From the game.py module:
bananas = {}		# Dictionary to contain bananasglobals = {}		# Dictionary to contain any needed info.bananacount = input('How many bananas do you wanna see? ')		## Get number of bananasglobals['banana_count'] = bananacountfor x in range(0, bananacount):						## Create bananas dictionary    banana_name = "banana" + str(x)    xPos = whrandom.randint(100,500)					## Random x position    yPos = whrandom.randint(100,500)				   	## Random y position    maturity = whrandom.randint(1,4)					## Random maturity    new_banana = flora2.Banana(banana_name, xPos, yPos, maturity)	## Class instance     bananas[banana_name] = new_banana					## Add instance to dictionary 
And from my flora.py module:
    def die(self, bananas, banana_name, canvas):	del bananas[banana_name]				## remove banana instance	canvas.delete(banana_name)				## remove picture from canvas	print banana_name, "has died." 
Thanks a lot, guys.  I'm starting to learn (a little) what the natural data structures are for some of these things.
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:
Pieces of my main module, game.py:
globals = {}		# Dictionary to contain any needed info.bananacount = input('How many bananas do you wanna see? ')		## Get number of bananasglobals['banana_count'] = bananacount####################### START THE GAME #########################window.GameWindow(heroes, bananas, globals)
>From the window.py module:
	gameframe = myFrame(gamewindow, heroes, bananas, globals)	## Create the main frame...        def __init__(self, gamewindow, heroes, bananas, globals):	        Tkinter.Frame.__init__(self, gamewindow)		        Tkinter.Pack.config(self)	        self.createMenus(gamewindow,heroes)	        self.createCanvas(gamewindow, heroes, bananas, globals)...	def createCanvas(gameframe, gamewindow, heroes, bananas, globals):		gameframe.moveImages(canvas, gameframe, gamewindow, heroes, bananas, globals)...	def moveImages(self, canvas, gameframe, gamewindow, heroes, bananas, globals):		while 1:		#### Update AI/ new objects				   for banana_name in bananas.keys():			bananas[banana_name].mature(canvas, bananas, globals)
And that's how I get the information to my bananas in flora.py.  Sorry for all of the code, here, but do you see how I have to include the globals dictionary all over the place, in a lot of places it's not needed, just to get it to the bananas functions?  I know that I could eliminate some of it by making the globals dictionary global within each module (and I should rename it "myGlobals" while I'm at it), but that's still a lot of passing around.
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 treatment of scope you've read?  90% of my problems arise from confusion about scope.
Thanks for listening to me ramble,Chris 


---------------------------------
Do You Yahoo!?
Yahoo! Mail Personal Address - Get email at your own domain with Yahoo! Mail.
--0-1728907065-984151198=:3915
Content-Type: text/html; charset=us-ascii

So, can you have a "significant rewrite" of a program that's fewer than 1000 lines?  

I got the chance yesterday to play with my virtual world program again, and tried some of the things suggested a couple of days ago.  For those of you late to the conversation, I was trying to do the following:
<p>
- Create a list.<br>
- Append instances of a class to the list.<br>
- Remove those instances without regard to their place in the list.
<p>
The list happened to contain bananas, which I wanted to remove when they died of old age or got eaten.
<p>
The obvious solution, now that I think about it (and mentioned by more than one person) was to grab the bananas by name, which implies the use of a dictionary.  Once I got the hang of how to refer to values inside items inside a dictionary, it was all smooth sailing:

<p><b>
>From the game.py module:</b>
<pre>

bananas = {}		# Dictionary to contain bananas
globals = {}		# Dictionary to contain any needed info.

bananacount = input('How many bananas do you wanna see? ')		## Get number of bananas
globals['banana_count'] = bananacount
for x in range(0, bananacount):						## Create bananas dictionary
    banana_name = "banana" + str(x)
    xPos = whrandom.randint(100,500)					## Random x position
    yPos = whrandom.randint(100,500)				   	## Random y position
    maturity = whrandom.randint(1,4)					## Random maturity
    new_banana = flora2.Banana(banana_name, xPos, yPos, maturity)	## Class instance 
    bananas[banana_name] = new_banana					## Add instance to dictionary 
</pre><b>
And from my flora.py module:</b>
<pre>
    def die(self, bananas, banana_name, canvas):
	del bananas[banana_name]				## remove banana instance
	canvas.delete(banana_name)				## remove picture from canvas
	print banana_name, "has died." 

</pre>

Thanks a lot, guys.  I'm starting to learn (a little) what the natural data structures are for some of these things.
<p>
As usual, I have a piggyback question:
<p>
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:
<p>
<b>Pieces of my main module, game.py:</b>
<pre>

globals = {}		# Dictionary to contain any needed info.

bananacount = input('How many bananas do you wanna see? ')		## Get number of bananas
globals['banana_count'] = bananacount

####################### START THE GAME #########################
window.GameWindow(heroes, bananas, globals)

</pre>

<b>From the window.py module:</b>
<pre>
	gameframe = myFrame(gamewindow, heroes, bananas, globals)	## Create the main frame
...
        def __init__(self, gamewindow, heroes, bananas, globals):
	        Tkinter.Frame.__init__(self, gamewindow)	
	        Tkinter.Pack.config(self)
	        self.createMenus(gamewindow,heroes)
	        self.createCanvas(gamewindow, heroes, bananas, globals)
...
	def createCanvas(gameframe, gamewindow, heroes, bananas, globals):
		gameframe.moveImages(canvas, gameframe, gamewindow, heroes, bananas, globals)

...

	def moveImages(self, canvas, gameframe, gamewindow, heroes, bananas, globals):
		while 1:
		#### Update AI/ new objects
		
		   for banana_name in bananas.keys():
			bananas[banana_name].mature(canvas, bananas, globals)

</pre>
And that's how I get the information to my bananas in flora.py.  Sorry for all of the code, here, but do you see how I have to include the globals dictionary all over the place, in a lot of places it's not needed, just to get it to the bananas functions?  I know that I could eliminate some of it by making the globals dictionary global within each module (and I should rename it "myGlobals" while I'm at it), but that's still a lot of passing around.
<p>
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 treatment of scope you've read?  90% of my problems arise from confusion about scope.
<p>
Thanks for listening to me ramble,
Chris



 <p><br><hr size=1><b>Do You Yahoo!?</b><br>
<a href="http://personal.mail.yahoo.com/?.refer=mailiyfoot">Yahoo! Mail Personal Address</a> - 
Get email at your own domain with Yahoo! Mail.
--0-1728907065-984151198=:3915--