Introduction 🌟
**Class Attributes** are variables that belong to the **class itself**, not to any specific object. They are **shared by all instances** of the class and are created outside of any method.
Note
1. Basic Example of Class Attributes 🧱
class_attr_basic.py
class Student:
school = "Govt School" # class attribute
s1 = Student()
s2 = Student()
print(s1.school) # Govt School
print(s2.school) # Govt School✔ Both objects share the same value from the class.
✔ Changing at class level affects all instances.
2. Changing Class Attribute Using Class Name 🏫
change_class_attr.py
class Student:
school = "Govt School"
print(Student.school)
Student.school = "Public School"
s1 = Student()
s2 = Student()
print(s1.school) # Public School
print(s2.school) # Public SchoolNote
3. Instance Attribute Overrides Class Attribute ⚔️
override_class_attr.py
class Student:
school = "Govt School"
s1 = Student()
s2 = Student()
s1.school = "Private School" # creates a new instance attribute
print(s1.school) # Private School
print(s2.school) # Govt School✔ Assigning to s1.school creates a NEW instance attribute (does NOT change class attribute).
4. Viewing Class Attributes Using __dict__ 🔍
dict_view.py
class Car:
wheels = 4
brand = "Generic"
print(Car.__dict__)✔ Shows all class-level attributes and methods.
5. Class Attributes vs Instance Attributes ⚖️
| Class Attribute | Instance Attribute |
|---|---|
| Shared by all objects | Unique to each object |
| Defined outside methods | Defined inside __init__() |
| Stored in class memory | Stored inside object |
| Same value for all instances | Different per instance |
6. Example: Counting Number of Objects Created 🧮
object_counter.py
class Counter:
count = 0 # class attribute
def __init__(self):
Counter.count += 1
c1 = Counter()
c2 = Counter()
c3 = Counter()
print(Counter.count) # 3✔ Perfect use of class attributes — shared, consistent counter.
7. Example: Shared Configuration 🔧
config_example.py
class AppConfig:
app_name = "MyApp"
version = "1.0"
u1 = AppConfig()
u2 = AppConfig()
print(u1.app_name)
print(u2.version)✔ All objects share the same configuration.
8. Using Class Attributes Inside Methods 🧠
inside_method.py
class Employee:
company = "Google"
def show_company(self):
print("Company:", Employee.company)
e = Employee()
e.show_company()Note
9. When to Use Class Attributes? 🎯
- ✔ When data must be shared by all objects
- ✔ When storing constants (e.g., PI, TAX_RATE)
- ✔ When configuration values should apply to all instances
- ✔ When tracking counts across all objects
- ✔ When making global settings inside a class
10. Real-World Example: Product Tax Calculation 💰
tax_example.py
class Product:
tax_rate = 0.18 # 18% GST
def __init__(self, price):
self.price = price
def total_price(self):
return self.price + (self.price * Product.tax_rate)
p = Product(1000)
print(p.total_price())✔ All products use the same tax rate.
11. Class Attribute Inside Constructor (Read Only) 🔐
constructor_class_attr.py
class Server:
server_name = "AWS"
def __init__(self, user):
self.user = user
print("Connecting to", Server.server_name)12. Common Mistakes ⚠️
- ❌ Modifying class attributes using objects — leads to unexpected behavior
- ❌ Using class attributes for values that should be unique to each object
- ❌ Forgetting that changing class attributes affects ALL instances
Conclusion 🎉
You now fully understand Class Attributes in Python! Want the next topic? Try Methods, Constructors, Inheritance, Encapsulation, or Polymorphism. Just tell me! 😊