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

Re: [pygame] Python enumerations



On 7/18/05, Dan <dan@xxxxxxxxxxxxxxxx> wrote:
> 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?

It depends a lot on how you are using them and and what particular
"features" of traditional enums you are after.

I've tried several things over the years, but currently I prefer just
using plain string literals for most enum-like work. The builtin
compile function is a good example of this: it's signature is
compile(string, filename, kind), where kind is simply one of the
strings "exec", "eval" or "single". No need to define constants called
COMPILE_EXEC, COMPILE_EVAL etc. when all checking is runtime anyway
(although if you rely on something like PyChecker, it can more easily
spot errors like using undefined constants).

For more fully featured, "real" enums, I prefer this cookbook recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486

-- 
Sami Hangaslammi