Python OOP Tutorial 2: Class Variables
Understanding Class Variables in Python
Introduction to Instance and Class Variables
- The video begins with a recap of creating a simple class and instances, focusing on instance variables that hold unique data for each instance.
- A brief mention of class variables is made, which are shared among all instances of a class, contrasting them with instance variables.
Use Case for Class Variables
- The speaker introduces the concept of annual raises as an example where class variables would be beneficial since the raise amount is consistent across all employees.
- Before implementing the class variable, the speaker hard codes the raise amount into a method called
apply_raiseto illustrate why using a class variable is advantageous.
Implementing Class Variables
- The
apply_raisemethod modifies an employee's pay by applying a 4% increase. This demonstrates how raises can be applied but highlights limitations in accessing the raise amount.
- The need for easy access to the raise amount through both instances and classes is emphasized; currently, it’s hard-coded within methods.
Transitioning to Class Variable Implementation
- To improve code maintainability, the speaker suggests moving the hard-coded 4% into a class variable defined at the top of the employee class.
- Accessing this new
raise_amountvariable requires referencing it through either the employee class or its instances.
Understanding Access to Class Variables
- An explanation follows about how accessing attributes works: if an instance does not have an attribute, Python checks if it's available in its parent class.
- Demonstrations show that both instances and classes can access
raise_amount, clarifying how inheritance affects attribute visibility.
Exploring Namespace and Attribute Visibility
- The speaker prints out various attributes from an instance to illustrate that while individual instances do not contain
raise_amount, they can still access it via their parent class.
- By examining namespaces further, it becomes clear that changes made to
raise_amountaffect all instances due to its shared nature as a class variable.
Conclusion on Modifying Class Variables
Understanding Instance and Class Variables in Python
Instance Variables vs. Class Variables
- The discussion begins with the concept of using instance variables, where
employee1.raise_amountis set to 5%, affecting only that specific instance.
- When checking
employee1's namespace, it shows that theraise_amountattribute was created specifically for this instance, demonstrating how instance variables can be unique to each object.
- The importance of understanding the distinction between instance and class variables is emphasized; different results may arise depending on whether
self.raise_amountorEmployee.raise_amountis used in methods.
- Using
self.raise_amountallows individual instances to have their own raise amounts while still enabling subclasses to override these values if necessary.
Tracking Class-wide Attributes
- A new example illustrates tracking the total number of employees as a class variable (
num_of_employees) initialized at zero, which should remain consistent across all instances.
- Each time a new employee is instantiated, the class variable increments by one within the
__init__method, ensuring that it reflects the total count accurately.
- It’s crucial to use
Employee.num_of_employees, rather thanself.num_of_employees, since there’s no scenario where individual instances would need differing employee counts.
Practical Demonstration and Conclusion
- After running tests with print statements before and after instantiation, it confirms that the total number of employees correctly updates based on how many were created (e.g., returning 2 after two instantiations).