[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library (original) (raw)

Ethan Furman ethan at stoneleaf.us
Sun Apr 28 08:37:24 CEST 2013


On 04/27/2013 08:59 PM, Greg Ewing wrote:

Ethan Furman wrote:

Overriding init is a PITA because init is also called when you do

Planet(3) # get EARTH and init was expecting a gravitational constant and radius (or something like that). A couple ways around that: 1) have the metaclass store the args somewhere special 2) have a init that the metaclass calls with the args instead of init I don't much like either of those. It would be much nicer if one could just write an ordinary init method and have it work as expected.

Agreed, on both counts.

It's possible to make it work, I think. The call method of the metaclass is going to have to do something special anyway, so that Planet(3) can look up and return an existing instance instead of making a new one. And if it doesn't make a new instance, it's not going to call the init method.

So far I've had that logic in new (which, of course, has no control on whether init is called); I'll check out call as soon as I can.

Thanks for the tip!

-- Ethan



More information about the Python-Dev mailing list