generic class for everything?

Carl Banks imbosol-1048875419 at aerojockey.com
Fri Mar 28 13:36:37 EST 2003


Rob Brown-Bayliss wrote:
> How could I go about designing an object for the list of possible
> effects? 
> 
> I had though of a class that has:
> 
> unique identifier
> name
> description
> stuff
> 
> where stuff is basically a large text field so item might be:
> 
> 1234
> "No Gas"
> "The fule tank has run dry"
> "fule(0) fule_light(on) power_out(-100%) electrical_out(-100%) lights(0)
> "
> 
> and then parse  the stuff field modifying the parent class.
> 
> The only other option I can think of is having the class have avariable
> for every single thing that can happen, but that seems to be a night
> mare
> 
> Any thoughts?

Two.

First, as you have it, stuff should be a dictionary:

    self.stuff = { 'fule': 0, 'fule_light': 'on', 'power_out': -100, ... }


Second, may I suggest using a function instead of a class?  Events, of
course, are actions, so it might make sense to define an event as a
function.  And, starting from Python 2.1, functions can have
attributes, so you can define the name an description fields.  For
example:

    def no_gas(car):
        car.fule = 0
        car.fule_light = 'on'
        car.reduce_power(-100)
        ...

    no_gas.name = "No Gas"
    no_gas.description = "The fuel tank has run dry."

This is just a suggestion; it might not be better for what you're
doing.  You'll have to decide.


-- 
CARL BANKS




More information about the Python-list mailing list