ProgramsPython

Object-Oriented Programs

classes, inheritance, a singleton nobody asked for. Read __init__ twice.

25 programs · open one, then the next

Start with Create Class & Object
  1. 01Create Class & ObjectBeginner__init__ runs when you call Student(...). display_info is just a function with self already filled in.Open example →
  2. 02Instance vs Class VariablesBeginnercount lives on the class. id lives on each object. Change count once, every instance sees it.Open example →
  3. 03Constructors in Python ClassesBeginnerUse the __init__ constructor to initialize new objects with default and custom values.Open example →
  4. 04Single InheritanceBeginnerDog(Animal) can speak() without defining it. That is the whole inheritance pitch.Open example →
  5. 05Multiple InheritanceIntermediateDuck(Flyer, Swimmer) gets methods from both. MRO decides who wins if names clash.Open example →
  6. 06Multilevel InheritanceBeginnerIllustrate inheritance across multiple levels of a class hierarchy.Open example →
  7. 07Polymorphism with MethodsBeginnerUse polymorphism by defining the same method name in different classes.Open example →
  8. 08Method Overloading SimulationBeginnerSimulate method overloading using default arguments and *args.Open example →
  9. 09Encapsulation with Getters/SettersIntermediateUse properties to encapsulate attribute access with getter and setter logic.Open example →
  10. 10Abstraction with ABCIntermediateUse the abc module to define abstract base classes and abstract methods.Open example →
  11. 11Operator OverloadingIntermediateOverload the + operator for a custom class using __add__.Open example →
  12. 12Custom IteratorIntermediateImplement a class that can be iterated over using __iter__ and __next__.Open example →
  13. 13Class MethodsIntermediateUse @classmethod to create alternative constructors.Open example →
  14. 14Static MethodsBeginnerUse @staticmethod for utility methods that logically belong to the class but do not use self or cls.Open example →
  15. 15Composition ExampleIntermediateUse composition by placing one object inside another to build complex behavior.Open example →
  16. 16Aggregation ExampleIntermediatePass an existing object in. The other class uses it; it does not create it.Open example →
  17. 17Private VariablesIntermediateUse name-mangling with double underscores to indicate private attributes.Open example →
  18. 18Magic Methods OverviewIntermediateShow common magic methods like __str__ and __len__.Open example →
  19. 19Object CloningIntermediateClone objects using the copy module (shallow and deep copy).Open example →
  20. 20Custom ExceptionsIntermediateDefine and raise custom exception classes in an OOP style.Open example →
  21. 21Class DecoratorIntermediateUse a class as a decorator to wrap functions with additional behavior.Open example →
  22. 22Prototype PatternIntermediateImplement a simple Prototype pattern by cloning existing objects.Open example →
  23. 23Singleton PatternIntermediateOnly one instance. Call the constructor twice, get the same object back. People overuse this.Open example →
  24. 24MRO (Method Resolution Order) DemoIntermediateClassName.__mro__ is the lookup order. Print it when diamond inheritance gets weird.Open example →
  25. 25Polymorphism (Duck Typing)IntermediateUse duck typing to write functions that work with any object having the required method.Open example →