Introduction
Designing efficient and readable Python classes is key when building real-world applications. A common example arises in employee payroll systems, where each employee has unique work hours, but all share the same hourly pay rate. You might define an Employee class with a method calculate_salary, but soon realize that while hours worked vary per instance, the hourly rate remains the same for all. So how do you structure your class to cleanly and correctly reflect this?

The Right Way to Structure It
At first, it might be tempting to include both hours_worked and hourly_rate as instance variables in the __init__ method:
class Employee:
def __init__(self, name, hours_worked, hourly_rate):
self.name = name
self.hours_worked = hours_worked
self.hourly_rate = hourly_rate
But this leads to duplication and possible inconsistency: why assign the same hourly rate to every instance when it’s a shared constant?
The better approach? Use a class variable for the hourly rate.
Class variables are shared across all instances of a class. They’re perfect for constants that don’t vary from one object to another.

Here’s a cleaner version:
class Employee:
hourly_rate = 20 # shared constant across all employees
def __init__(self, name, hours_worked):
self.name = name
self.hours_worked = hours_worked
def calculate_salary(self):
return self.hours_worked * Employee.hourly_rate
Now, each Employee instance only stores what is unique to it—its name and hours worked—while the shared hourly_rate remains consistent and accessible.

Conclusion
When structuring your Python classes, it’s important to separate instance-specific data from shared constants. In the case of the Employee class, using a class variable for hourly_rate ensures cleaner code, avoids redundancy, and improves maintainability. This small design choice reflects a broader principle in object-oriented programming: use the right type of variable (instance or class) based on whether the data is unique or shared. It’s these thoughtful design choices that elevate your code from functional to elegant.