[Python-ideas] An even simpler customization of class creation
Nick Coghlan
ncoghlan at gmail.com
Fri Mar 6 14:21:36 CET 2015
On 4 March 2015 at 03:28, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 03/02/2015 02:57 AM, Martin Teichmann wrote:
>> class Soliton(type):
>> def __new__(cls, name, bases, ns):
>> self = super().__new__(name, bases, ns)
>> return self()
>>
>> class A(metaclass=Soliton):
>> def f(self):
>> print(__class__)
>
> This should be `self.__class__`, yes?
No, it's printing out the type referenced from the definition time
cell*, rather than the type of the object actually passed in to the
method.
Cheers,
Nick.
P.S. *For anyone that isn't already aware, the general gist of how that works:
* The compiler actually special cases "super()" and "__class__" in method bodies
* If it sees either of them, it implicitly adds a "__class__" cell
reference to the function
* In the "super()" case, the call is implicitly rewritten as
"super(<first param>, __class__)"
* __class__ is later filled in with a reference to the class being
defined, once that actually exists
In my view, that's never going to win any awards for "elegance in
language design", but it makes super() so much easier to use I'm happy
to tell my sense of aesthetics to be quiet :)
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list