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

Re: [pygame] When to use a constant



Sebastien Migniot wrote:
Hi,

 Right, if you're looking for abstraction use a class,
 if you just *want it to work*, use arrays.

 suggestion:

 If you use a class, try to define operations on it,
 such as __mul__ and __add__, so that you effectively
 *do not have* to access self[0] and self[1] anymore

 AFAIC, I would keep the array and no constants
 for as long as my 'Velocity' objects do not require
 member methods.
Using a class is an interesting idea, which I may look into further. Right now, however, my code involves a number of for-loops and list comprehensions which can act on any set of components (which is the only reason I'm using a list) and I'm not sure how I could replace them without introducing a lot of duplicate code.

As an example, I have a lot of constructs similar to this:

def apply_force(self, force, interval):
"""Return the change in velocity caused by a force.
"""

return [(force[n] / self.mass) * interval for n in range(2)]

And while there's plenty of ways to do the same thing, I can't think of any that are nearly as simple and clean. Maybe I just like list comprehensions too much :).

--
Jon Doda