Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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.

Tehtävä

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

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?

close

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

Tehtävä

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

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2
single

single

some-alt