InterviewHubPRO
Section Overview
Q17

Question 17 of 65

Part 2: Python Core Language, Internals & Runtime (6 - 25)

26%
Python & FastAPI•Part 2: Python Core Language, Internals & Runtime (6 - 25)

Metaclasses in Python:

In Simple Words: A metaclass is a "class of a class" that defines how a class is constructed. Classes are instances of metaclasses (default is type). Used in ORMs (Django models) and Pydantic.

python
class EnforceUpperMeta(type):
    def __new__(mcs, name, bases, attrs):
        # Convert all custom attribute names to uppercase:
        upper_attrs = {k.upper() if not k.startswith("__") else k: v for k, v in attrs.items()}
        return super().__new__(mcs, name, bases, upper_attrs)

class Config(metaclass=EnforceUpperMeta):
    port = 8080

print(Config.PORT) # 8080