[Python-ideas] A way out of Meta-hell (was: A (meta)class algebra)

Antony Lee antony.lee at berkeley.edu
Thu Feb 19 20:39:36 CET 2015


In practice, this can be worked around using a method on the metaclass,
defined as follows (basically following Nick's idea):

    def _super_init(cls, name):
        super_cls = super(sys._getframe(1).f_globals.get(name, cls), cls)
        try:
            init_class = super_cls.__init_class__
        except AttributeError:
            pass
        else:
            init_class()

Now call cls._super_init() whenever you wanted to call super().__init__().

Antony

2015-02-19 4:28 GMT-08:00 Martin Teichmann <lkb.teichmann at gmail.com>:

> > Yes, populating __class__ happens inside the interpreter only after the
> > class is fully constructed, so even though the *class* is fully
> initialised
> > at the end of __init__, a reference hasn't been inserted into the cell
> yet.
> > (The __class__ cell doesn't actually live on the class object - it's
> created
> > implicitly by the interpreter and then referenced from the individual
> > methods as a closure variable for each method that looks up "__class__"
> or
> > "super")
> >
> > Python 2 doesn't provide the implicitly populated __class___ cell at all
> > though, so you have to refer to the class object by its name - zero
> argument
> > super is a Python 3 only feature.
>
> In short: PEP 422 in the current form is not back-portable.
> With my modifications it is portable all the way down to python 2.7.
>
> PEP 422 being back-portable => people will immediately start using it.
> PEP 422 not back-portable => it won't be used for several years to come.
>
> Greetings
>
> Martin
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150219/6d62a52a/attachment.html>


More information about the Python-ideas mailing list