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

single

bookChallenge: Enforcing Attribute Naming Conventions

Swipe to show 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.

Task

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.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 2. Chapterย 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt