Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Learn Python NESTED CLASSES in 9 minutes! ๐Ÿ“›
Play lesson

Python tutorial for beginners ๐Ÿ - Learn Python NESTED CLASSES in 9 minutes! ๐Ÿ“›

4.0 (0)
8 learners

What you'll learn

This course includes

  • 14.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

# Nested class = A class defined within another class # class Outer: # class Inner: # Benefits: Allows you to logically group classes that are closely related # Encapsulates private details that aren't relevant outside of the outer class # Keeps the namespace clean; reduces the possibility of naming conflicts class Company: class Employee: def __init__(self, name, position): self.name = name self.position = position def get_details(self): return f"{self.name} {self.position}" def __init__(self, company_name): self.company_name = company_name self.employees = [] def add_employee(self, name, position): new_employee = self.Employee(name, position) self.employees.append(new_employee) def list_employees(self): return [employee.get_details() for employee in self.employees] company1 = Company("Krusty Krab") company2 = Company("Chum Bucket") company1.add_employee("Eugene", "Manager") company1.add_employee("Spongebob", "Cook") company1.add_employee("Squidward", "Cashier") company2.add_employee("Sheldon", "Manager") company2.add_employee("Karen", "Assistant") for employee in company2.list_employees(): print(employee)

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere