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~
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com