Create a Class with type()
Advanced · Write the code · from Python Metaclasses: Control How Classes Are Created
From the lesson: Python Metaclasses: Control How Classes Are Created
You already know that classes create objects. But have you ever wondered - what creates the classes themselves? In Python, the answer is metaclasses.
Read the lessonUse the type() function (not a class statement) to create a class called Counter with:
count initialized to 0increment(self) that increases count by 1 and returns the new valueThen create an instance, call increment() three times, and print the final count.
# Create the Counter class using type()
# Then create an instance and call increment() 3 times
More Metaclass exercises
- Predict the Metaclass OutputAdvanced
- Build a Validation MetaclassAdvanced
- Fix the Plugin RegistryAdvanced
- Build a Field-Discovery MetaclassAdvanced
All Python exercises · 50 practice problems · Metaprogramming quiz