[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Victor Stinner victor.stinner at gmail.com
Sat Apr 27 10:03:13 CEST 2013


Why not defining new methods/changing the behaviour using a different
metaclass?

Victor
Le 27 avr. 2013 05:12, "Nikolaus Rath" <Nikolaus at rath.org> a écrit :

> Steven D'Aprano <steve at pearwood.info> writes:
> > On 26/04/13 13:22, Greg wrote:
> >> On 26/04/2013 3:12 p.m., Glenn Linderman wrote:
> >>> On 4/25/2013 7:49 PM, Nick Coghlan wrote:
> >>
> >>>> You couldn't create an enum of callables, but that would be a
> >>>> seriously weird thing to do anyway....
> >>>
> >>> But aren't all classes callable?
> >>
> >> An enum of classes would be seriously weird as well, I think.
> >
> >
> > I don't think iscallable will work, since that descriptors like
> > staticmethod and classmethod aren't callable. Nor are properties.
> >
> >
> > I think a solution may be an explicit decorator that tells the
> > metaclass not to skip the object into an enum value:
> >
> >
> > class Insect(enum.Enum):
> >     ant = 1
> >     bee = 2
> >
> >     @enum.skip
> >     @classmethod
> >     def spam(cls, args):
> >         pass
>
>
> In this case, wouldn't it be nicer to "decorate" those attributes that
> are meant to be enum values? I think having to use the class keyword to
> define something that really doesn't behave like an ordinary class is
> pretty confusing, and the following syntax would be a lot easier to
> understand at first sight:
>
> class Insect(enum.Enum):
>     ant = enum.EnumValue(1)
>     bee = enum.EnumValue(2)
>
>     @classmethod
>     def spam(cls, args):
>          pass
>
>     def ham(self, args):
>          pass
>
>
> Obviously, EnumValue() would automatically assign a suitable number.
>
>
> Best,
>
>    -Nikolaus
>
> --
>  »Time flies like an arrow, fruit flies like a Banana.«
>
>   PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/victor.stinner%40gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130427/290ba527/attachment-0001.html>


More information about the Python-Dev mailing list