On Thu, Apr 7, 2011 at 11:48 PM, Michael Foord <fuzzyman@gmail.com> wrote:That's the dance I'm trying to remember. You make it work by playing
> Hmmm... that would rely on subclass.__new__ both existing *and* not calling
> up to its parent __new__ or you will have infinite recursion.
> Probably what you have to do is call object.__new__(subclass, ...) and
> knowing / praying that subclass.__new__ doesn't do anything important...
identity checking games with the cls argument, but it's been
absolutely ages since I read about it and experimented with it.
I think it's something like:
def __new__(cls, *args, **kwds):
if cls is ThisClass:
# Do fancy footwork to implicitly create an appropriate subclass instead
# via subclass.__new__
obj = cls._create_instance(*args, **kwds)
else:
# Don't do anything tricky for subclasses, that's their problem
obj = object.__new__(*args, **kwds)
return obj
Subclasses then have the option of passing the parent class as "cls"
if they want to invoke the fancy footwork, or themselves if they
don't.
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html