Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Logging Class Creation | Custom Metaclasses in Action
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Metaclasses Demystified

bookChallenge: 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.

Aufgabe

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.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookChallenge: Logging Class Creation

Swipe um das Menü anzuzeigen

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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
single

single

some-alt