Class methods and cls usage
📖 Scenario: Imagine you are creating a simple system to track how many books have been added to a library. Each book has a title and an author. You want to keep count of all books created using a class method.
🎯 Goal: You will build a Book class that keeps track of the total number of books created using a class method and the cls keyword.
📋 What You'll Learn
Create a class called
Book with attributes title and authorAdd a class variable
count to track the number of booksCreate a class method called
increment_count that increases count by 1 using clsUse the class method inside the constructor to update the count each time a new book is created
Print the total number of books using the class variable
💡 Why This Matters
🌍 Real World
Tracking counts of objects like books, users, or orders is common in software. Class methods help manage data shared across all objects.
💼 Career
Understanding class methods and class variables is important for writing clean, organized code in many programming jobs, especially in object-oriented programming.
Progress0 / 4 steps