Special Method Protocols

__prepare__() — class creation protocol

Metaclass hook creating the namespace used while executing the class body. Typically defined with @classmethod. Return a mapping compatible with the metaclass construction step.

Formal Syntax & Structure

python syntaxpython
def __prepare__(mcls, name, bases, **kwargs):

Parameters & Attributes

NameTypeDescription
namestrClass name.
basestupleResolved base classes.
**kwargsdictClass keywords.

Return Value

mapping

Class-body namespace.

Working Example

Example Snippetpython
class Meta(type):    @classmethod    def __prepare__(mcls, name, bases, **kwargs): return {"label": name.lower()}class Report(metaclass=Meta):    count=3print(Report.label)
Python Reference