[Python-ideas] An even simpler customization of class creation

Nick Coghlan ncoghlan at gmail.com
Mon Mar 2 13:25:31 CET 2015


On 2 March 2015 at 21:46, Steven D'Aprano <steve at pearwood.info> wrote:
> On Mon, Mar 02, 2015 at 11:57:09AM +0100, Martin Teichmann wrote:
>> This is currently correct behavior, but I think it actually is not what one
>> expects, nor what one desires. (Does anyone out there make use of
>> such a construct? Please speak up!)
>
> I do now! Seriously, I think you have just solved a major problem for
> me. I'll need to do some further experimentation, but that is almost
> exactly what I have been looking for. I've been looking for a way to
> have a class statement return a custom instance and I think this might
> just do it.

Martin's question wasn't about using __new__ on the metaclass to do
factory function magic (that's "normal usage" as far as metaclasses
go). The "construct" in question here is:

1. Returning something from a metaclass __new__ implementation other
than the result of type.__new__; AND
2. Relying on __class__ to refer to the returned object, rather than
the result of type.__new__

That seems likely to be a vanishingly small subset of Python usage,
and even if it does happen, the subset that couldn't switch to using
explicit super() via the bound name is likely to be even smaller.

That seems like a risk well worth taking when the pay-off is having
class methods that use implicit super() just work when called from the
metaclass.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list