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

Re: [pygame] Python enumerations



------ Original Message ------
> But which way is best? Does anyone have experience with several and want
> to recommend something? In particular, does using non-integer
> enumerations have a measurable impact on performance?
> 
I use strings as the native type of enums, and I love it - one subtle (but
very important for my needs) advantage over integers is that serialized
objects that use them don't get bad data in them if I change the number of
options in the enum (like in your APPLE, PEACH example, if I serialize
something set as PEACH and it stores 2, what happens when I add BANANA and
PEACH becomes 3?).

As far as performance goes, I've used them for state machine stuff (so it
compares a few times per frame per object) and for setting properties from
config file things, (so nothing very inner-loopish) and it's never been a
hotspot. And I figure that if it's ever a problem, I can write something
special for that one problem case.

As far as how complicated of an enum works well, it really depends on the
features you want. For me only 3 features have ever been important
1. making it so serialized data doesn't get "broken" by code changes
unnecessarily (the thing above)
2. making so you access the elements of the enum as members of some enum
object (i.e. fruits.Peach instead of PEACH) so the code is more readable
3. making the enum object provide dictionary type access (__iter__ and __len__
and that stuff) so that I can make code that validates and enumerates possible
choices, like gui's that let you pick an option from an enum and all that

so I just wrote something as simple as I could make it to do those 3 things.
In my case having enum elements represented by strings seemed the best way to
go.