Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Enforcing Attribute Naming Conventions | Custom Metaclasses in Action
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Metaclasses Demystified

bookChallenge: 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.

Tarefa

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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookChallenge: 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.

Tarefa

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

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
single

single

some-alt