[Tutor] A more Pythonic way to do this

Brian van den Broek bvande at po-box.mcgill.ca
Fri Jul 1 01:31:37 CEST 2005

D. Hartley said unto the world upon 30/06/2005 18:11:
> Hey guys!
> I have a 'quest,' and at first glance this email looks long, but the
> problem is probably not as complex as the length implies.  Please bear
> with me, if I could get some advice on this particular problem, it
> would go along way toward helping me be a better Python programmer.
> Some of you might remember, a few months ago, I was working on a space
> invaders game for a birthday present.  One of the functions I really
> wanted to add to the game was to change the enemy "ships" at each new
> level. Because of my deadline and my newness to Python, I did this in
> an extremely ugly way.  To give you an idea:

<snip lots of detail>

Hi Denise,

I sympathize; I'm one of those on the list given to asking long 
questions, too. From what you describe, you are right to think there 
is a smell you ought fix. I don't have a sound enough grasp myself to 
be useful for the details. But, I wanted to say something about:

> But now it's giving me an error when createEnemies is called, for the
> enemyship_sprites.add(Enemy((cols*60)+20, (rows*40)+30), level) line,
> saying __init__() (and this would be Enemy's init, correct?) takes
> exactly 4 arguments (3 given):  


If you are sure which __init__() is at issue, print is your friend. 
I've found that when my class hierarchy isn't matching my 
expectations, things like:

class A(object):
     def __init__(self):
         print "In A.__init__()"
         # real logic here
     def act(self):
         print "In A.act()"
etc. can really help out.

I don't know if that'll be enough to push you to seeing the problem, 
but it can only help :-)


Brian vdB

More information about the Tutor mailing list