Unit: Unit 7: Introduction to Classes • Interactive Practice
Interactive Python programming tutorial and practice in your browser.
# Base Employee Class
class Employee:
def __init__(self, name, emp_id, salary):
self.name = name
self.emp_id = emp_id
self.salary = salary
def get_details(self):
return f"[{self.emp_id}] {self.name} - Base Salary: ${self.salary:,}"
# Derived Manager Class adding team size and bonus calculation
class Manager(Employee):
def __init__(self, name, emp_id, salary, team_size):
# 1. Delegate core attribute initialization to parent class via super()
super().__init__(name, emp_id, salary)
# 2. Initialize manager-specific attribute
self.team_size = team_size
# Extend parent get_details() with team information
def get_details(self):
base_info = super().get_details()
return f"{base_info} (Direct Reports: {self.team_size})"
# Instantiate Manager and print composite details
mgr = Manager("Sarah Connor", "MGR-501", 120000, 8)
print(mgr.get_details())