(original) (raw)
Date: Fri, 13 Oct 2017 08:57:00 -0700
From: Guido van Rossum <guido@python.org>
To: Martin Teichmann <lkb.teichmann@gmail.com>
Cc: Python-Dev <python-dev@python.org>
Subject: Re: \[Python-Dev\] What is the design purpose of metaclasses vs
code generating decorators? (was Re: PEP 557: Data Classes)
Message-ID:
<CAP7+vJKBVuDqf09zTWDAuvQ-cCNM+cF82c22s2NJOj+A9k7\_kA@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
This is food for thought. I'll have to let it sink in a bit, but you may be
on to something.
Since the question was asked at some point, yes, metaclasses are much older
than class decorators. At some point I found the book Putting Metaclasses
to Work by Ira Forman and Scott Danforth (
https://www.amazon.com/Putting-Metaclasses-Work-Ira-Forman/dp/0201433052)
and translated the book's ideas from C++ to Python, except for the
automatic merging of multiple inherited metaclasses.
But in many cases class decorators are more useful.
I do worry that things like your autoslots decorator example might be
problematic because they create a new class, throwing away a lot of work
that was already done. But perhaps the right way to address this would be
to move the decision about the instance layout to a later phase? (Not sure
if that makes sense though.)
\--Guido
Just FYI, recreating the class with slots runs into problems with regards to PEP 3135 (New Super). In attrs we resort to black magic to update the \_\_class\_\_ cell in existing methods. Being able to add slotness later would be good, but not that useful for us since we have to support down to 2.7\.