[Python-Dev] constant/enum type in stdlib

Michael Foord fuzzyman at voidspace.org.uk
Wed Nov 24 18:41:08 CET 2010


On 24/11/2010 14:08, Nick Coghlan wrote:
> On Wed, Nov 24, 2010 at 10:30 PM, Michael Foord
> <fuzzyman at voidspace.org.uk>  wrote:
>> Based on a non-exhaustive search, Python standard library modules currently
>> using integers for constants:
> Thanks for that review. I think following up on the "NamedConstant"
> idea may make more sense than pursuing enums in their own right. That
> way we could get the debugging benefits on the Python side regardless
> of any type constraints on the value (e.g. needing to be an integer in
> order to interface to C code), without needing to design an enum API
> that suited all purposes.

Can you explain what you see as the difference?

I'm not particularly interested in type validation but I like the fact 
that typical enum APIs allow you to group constants: the generated 
constant class acts as a namespace for all the defined constants.

Are you just suggesting something along the lines of:

class NamedConstant(int):
def __new__(cls, name, val):
return int.__new__(cls, val)

def __init__(self, name, val):
self._name = name

def __repr__(self):
return '<NamedConstant %s>' % self._name

FOO = NamedConstant('FOO', 3)

In general the less features the better, but I'd like a few more 
features than that. :-)

All the best,

Michael


> Cheers,
> Nick.
>


-- 

http://www.voidspace.org.uk/

READ CAREFULLY. By accepting and reading this email you agree,
on behalf of your employer, to release me from all obligations
and waivers arising from any and all NON-NEGOTIATED agreements,
licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap,
confidentiality, non-disclosure, non-compete and acceptable use
policies (”BOGUS AGREEMENTS”) that I have entered into with your
employer, its partners, licensors, agents and assigns, in
perpetuity, without prejudice to my ongoing rights and privileges.
You further represent that you have the authority to release me
from any BOGUS AGREEMENTS on behalf of your employer.



More information about the Python-Dev mailing list