Challenge: Enforcing Attribute Naming Conventions
As you continue exploring Python metaclasses, recall that the class attribute dictionary—attrs in the __new__ or __init__ method of a metaclass—contains all attributes defined directly in the class body. Manipulating or inspecting this dictionary enables you to enforce rules or transform class attributes as needed. For example, you can iterate through attrs.items() to examine attribute names and values before the class is actually created. This capability is central to many metaclass-driven customizations, such as validation, transformation, or automatic documentation.
Swipe to start coding
Write a metaclass that enforces all class attribute names, except special methods (those surrounded by double underscores), are uppercase. If an attribute defined in the class body is not uppercase and does not start and end with double underscores, raise a ValueError. The error message should include both the attribute name and the class name.
Solução
Obrigado pelo seu feedback!
single
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 4.76
Challenge: Enforcing Attribute Naming Conventions
Deslize para mostrar o menu
As you continue exploring Python metaclasses, recall that the class attribute dictionary—attrs in the __new__ or __init__ method of a metaclass—contains all attributes defined directly in the class body. Manipulating or inspecting this dictionary enables you to enforce rules or transform class attributes as needed. For example, you can iterate through attrs.items() to examine attribute names and values before the class is actually created. This capability is central to many metaclass-driven customizations, such as validation, transformation, or automatic documentation.
Swipe to start coding
Write a metaclass that enforces all class attribute names, except special methods (those surrounded by double underscores), are uppercase. If an attribute defined in the class body is not uppercase and does not start and end with double underscores, raise a ValueError. The error message should include both the attribute name and the class name.
Solução
Obrigado pelo seu feedback!
single