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~
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4