Enum vs OrderedEnum

Ian Kelly ian.g.kelly at gmail.com
Wed Aug 7 03:35:28 EDT 2013


On Tue, Aug 6, 2013 at 6:33 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> class Environment(AutoNumber):
>
>     gaia = 2.0
>     fertile = 1.5
>     terran = 1.0
>     jungle = 1.0
>     ocean = 1.0
>     arid = 1.0
>     steppe = 1.0
>     desert = 1.0
>     minimal = 1.0
>     barren = 0.5
>     tundra = 0.5
>     dead = 0.5
>     inferno = 0.5
>     toxic = 0.5
>     radiated = 0.5
>
>     def __init__(self, growth_factor):
>         self._growth_factor = growth_factor
>
>     @property
>     def growth_factor(self):
>         return self._growth_factor
>
> This works because each Enum member gets its own integer value (1 - 15) in
> __new__, plus a growth factor that is stored by __init__.  Whether you think
> this is better I have no idea.  ;)

Black magic, I like it.  I think I'd write it like this, however:

class Environment(AutoNumber):

    gaia = 2.0
    fertile = 1.5
    terran = jungle = ocean = arid = steppe = desert = minimal = 1.0
    barren = tundra = dead = inferno = toxic = radiated = 0.5

    def __init__(self, growth_factor):
        self.growth_factor = growth_factor



More information about the Python-list mailing list