Challenge: Logging Class Creation
Now that you have learned how to define custom metaclasses, you can use their __new__ or __init__ methods to run code every time a new class is created. By placing a print statement in either of these methods, you can observe when and how Python constructs classes that use your metaclass. This is a powerful technique for debugging, inspecting, or instrumenting class creation in complex codebases, as it gives you visibility into the metaclass machinery at work.
Swipe to start coding
Create a metaclass named LoggingMeta that prints the name of every class it creates. When you define a class using LoggingMeta as its metaclass, it must print a message in the format Creating class: <ClassName>, where <ClassName> is replaced with the name of the class being defined. The message must be printed only during class creation, not when creating instances of the class.
Ratkaisu
Kiitos palautteestasi!
single
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you show an example of a custom metaclass with a print statement?
How do I choose between using __new__ and __init__ in a metaclass?
What are some practical use cases for custom metaclasses?
Mahtavaa!
Completion arvosana parantunut arvoon 4.76
Challenge: Logging Class Creation
Pyyhkäise näyttääksesi valikon
Now that you have learned how to define custom metaclasses, you can use their __new__ or __init__ methods to run code every time a new class is created. By placing a print statement in either of these methods, you can observe when and how Python constructs classes that use your metaclass. This is a powerful technique for debugging, inspecting, or instrumenting class creation in complex codebases, as it gives you visibility into the metaclass machinery at work.
Swipe to start coding
Create a metaclass named LoggingMeta that prints the name of every class it creates. When you define a class using LoggingMeta as its metaclass, it must print a message in the format Creating class: <ClassName>, where <ClassName> is replaced with the name of the class being defined. The message must be printed only during class creation, not when creating instances of the class.
Ratkaisu
Kiitos palautteestasi!
single