[Python-Dev] What is the design purpose of metaclasses vs code generating decorators? (was Re: PEP 557: Data Classes) (original) (raw)
Ethan Furman ethan at stoneleaf.us
Fri Oct 13 15:10:50 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] What is the design purpose of metaclasses vs code generating decorators? (was Re: PEP 557: Data Classes)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 10/13/2017 02:35 AM, Martin Teichmann 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. I am that nobody. The examples you give would be much nicer solved with decorators.
The same holds for enums. Inheriting from enums is possible, but weird, given that you cannot add new enums to it. So, especially when comparing to the dataclasses, the following looks appealing to me:
@enum class Breakfast: spam = 0 ham = 1
Things that will not work if Enum does not have a metaclass:
list(EnumClass) -> list of enum members dir(EnumClass) -> custom list of "interesting" items len(EnumClass) -> number of members member in EnumClass -> True or False
- protection from adding, deleting, and changing members
- guards against reusing the same name twice
- possible to have properties and members with the same name (i.e. "value" and "name")
--
Ethan
- 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] What is the design purpose of metaclasses vs code generating decorators? (was Re: PEP 557: Data Classes)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]