[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] main game object using singleton/global/static?



There's not alot you can do about 'self' in general, but if you reference a particular attribute or function many times you can avoid self by creating a local reference to the attribute or function.  In your example could replace the last two lines with:

game = self.game
game.playsound ("boom")
game.spawn ("item")

Of course, this would be a much more useful optimisation in cases with many more references to 'self.game' within a single function or in cases where 'self.game' appears in a loop.

What it really sounds like you are looking for is something like Pascal's 'with' statement, which basically treats a record or object's attributes as locals.  Python's 'with' statement has an entirely different function and purpose, of course. . .

On Wed, Jul 22, 2009 at 4:01 PM, Jake b <ninmonkeys@xxxxxxxxx> wrote:
Is there a shortcut or easier way than what I am doing? Because having to use the same reference in every object seems redundant?

I am using a 

I feel I am spamming "self." A lot of my classes have a lot of "self." lines Example of random code:

class Unit():
def onCollide(self, other):
if self.modifiedHP() - other.damage() < 0:
self.die()
self.game.playsound("boom")
self.game.spawn("item")

So I'm looking for advice on if this is normal? Or bad code?
--
Jake



--
--Ostsol

http://cheesesun.blogspot.com/