tonys / myspleenklug.on.ca (tony summerfelt) writes:

> and this is the code that init's all the planets:
> 
> 
> #!/usr/bin/python
> 
> import string, gebplanet, gebfleet,time
> 
> for letter in string.lowercase:
>     exec '%s=gebplanet.planet()' % letter
>     exec '%s.name(letter)' % letter
> 
> 
> later on when i need planet upkeep:
> 
> for letter in string.lowercase:
>     exec '%s.upkeep' % letter
> 
> 
> 
> 
> nothing fancy. no arrays, or hashes, or hashes of arrays, etc. the python
> code is very clean, readable, but most of all logical. i'm hoping the finished 
Using a hash is just as clean, but reduces the likelihood that you
destroy a planet inadvertently with a "for i in..."

   for letter in 'a'..'z'
     planets[letter] = GebPlanet.new(letter)
   end

and then

   planets.each {|p| p.upkeep}

You'll also find this is a lot faster, as there's no runtime
evaluation of strings.


Regards

Dave