[Python-Dev] What is the design purpose of metaclasses vs code generating decorators? (was Re: PEP 557: Data Classes) (original) (raw)
Random832 random832 at fastmail.com
Fri Oct 13 17:02:13 EDT 2017
- Previous message (by thread): [Python-Dev] What is the design purpose of metaclasses vs code generating decorators? (was Re: PEP 557: Data Classes)
- Next message (by thread): [Python-Dev] Scrapy Question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Fri, Oct 13, 2017, at 02:30, Nick Coghlan wrote:
Metaclasses currently tend to serve two distinct purposes:
1. Actually altering the runtime behaviour of a class and its children in non-standard ways (e.g. enums, ABCs, ORMs) 2. Boilerplate reduction in class definitions, reducing the amount of code you need to write as the author of that class Nobody has a problem with using metaclasses for the first purpose - that's what they're for. It's the second use case where they're problematic, as the fact that they're preserved on the class becomes a leaky implementation detail, and the lack of a JIT in CPython means they can also end up being expensive from a runtime performance perspective.
What about a metaclass that isn't a metaclass? A metaclass can be any callable and can return any object, e.g. a normal type.
def AutoSlotMeta(name, bases, dct, real_metaclass=type): """turn all class variables into slots""" dct['slots'] = list(dct) return real_metaclass(name, bases, dct)
- Previous message (by thread): [Python-Dev] What is the design purpose of metaclasses vs code generating decorators? (was Re: PEP 557: Data Classes)
- Next message (by thread): [Python-Dev] Scrapy Question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]