[Python-ideas] PEP-3151 pattern-matching
Nick Coghlan
ncoghlan at gmail.com
Thu Apr 7 16:36:39 CEST 2011
On Thu, Apr 7, 2011 at 11:48 PM, Michael Foord <fuzzyman at gmail.com> wrote:
> 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...
That's the dance I'm trying to remember. You make it work by playing
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.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list