Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Enforcing Attribute Naming Conventions | Custom Metaclasses in Action
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.

Tarea

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.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you give an example of how to use a metaclass to enforce attribute rules?

What are some common use cases for manipulating the class attribute dictionary in a metaclass?

How does modifying the attrs dictionary affect the resulting class?

close

bookChallenge: Enforcing Attribute Naming Conventions

Desliza para mostrar el menú

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.

Tarea

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
single

single

some-alt