Skip to main content

Command Palette

Search for a command to run...

Object-Oriented Programming: A Simple Walkthrough

Published
4 min readView as Markdown

This week, we dived into the world of Object-Oriented Programming (OOP) and iterators in Python. At first, it sounded like a heavy topic, but step by step we broke it down into simple, practical lessons. By the end of the week, we had explored everything from creating classes and objects to inheritance, polymorphism, iterators, and even how Python decides which method to run when multiple classes are involved.

Here’s a recap of what we learned:

1. Fundamentals of Classes and Objects

Python is an object-oriented programming language, Python data types belongs to an object of a corresponding built-in class. A class is the blueprint for creating an object, it defines the attributes and behaviour of an object, while an object is an instance of a class.

Python treats everything as an object. Numbers, strings, lists, dictionaries — all of them belong to classes. For example:

Python 3.9.6 (default, Jun 28 2021, 15:26:21)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> num = 10
>>> type(num)
<class 'int'>
>>> string = 'string'
>>> type(string)
<class 'str'>
>>> boolean = True
>>> type(boolean)
<class 'bool'>
>>> lst = []
>>> type(lst)
<class 'list'>
>>> tpl = ()
>>> type(tpl)
<class 'tuple'>
>>> set1 = set()
>>> type(set1)
<class 'set'>
>>> dct = {}
>>> type(dct)
<class 'dict'>

From the image above we can confirm the all python data type belongs to a class.

Creating a Class

To create a class we need the keyword class followed by the name and colon. Class name should be CamelCase.

Creating and Instantiating a class in Python

  • __str__() method: Used to control what is returned when an object is represented as a string.

  • Object creation: Simple classes were defined and instantiated, such as Person, ensuring attributes like name and age could be neatly represented.

  • Deleting objects: Demonstrated with del person.

This built a solid foundation for understanding how objects are created and manipulated.

2. Inheritance and Polymorphism

Next, we moved to one of the most powerful ideas in OOP: inheritance.

Inheritance allows us to define a class that inherits the methods and properties from parent class. The parent class or super or base class is the class which gives all the methods and properties. Child class is the class that inherits from another or parent class. Several tasks explored the power of inheritance, allowing classes to reuse and extend functionality:

  • Animal hierarchy: Demonstrated single inheritance, where Dog and Cat inherits properties rom the Animal class. The base class Animal was extended into Dog and Cat, with each subclass overriding the make_sound() method (Woof! and Meow!).

We use super() built-in function or the parent name Animal to automatically inherit the methods and properties from its parent. In the example above we override the parent method. The child method has a different method, it can identify, if the animal is dog or cat and assign the proper sound (Meow/Woof).

This exercises also highlighted polymorphism — the ability of different classes to implement methods in ways appropriate to their specific behaviors.

3. Iterators and iterables

The next topic we discussed about is iterators. An iterator is an object that implemented the __iter__ () and __next__ () method.

  • Iterator protocol: They can be created by explicitly using classes or by calling iter() on an iterable, An iterable is any object that can be looped over (e.g., string, list, tuple).

  • Practical examples:

    • Created a CountDown iterator counting backward from n to 1.

These tasks showed how iteration can be customized for specific use cases — from finite sequences like countdowns.

4. Scope and MRO

A variable is only available from inside the region it is created, this is called scope. A variable created inside a function belongs to the local scope of that function, and can only be used inside that function. A variable created in the main body of the Python code is a global variable and belongs to the global scope. Global variables are available from within any scope, global and local. If you need to create a global variable, but are stuck in the local scope, you can use the global keyword.

MRO stands for Method Resolution Order. It is the order in which Python looks for a method in a hierarchy of classes. MRO follows a specific sequence to determine which method to invoke when it encounters multiple classes with the same method name. In multiple inheritances, the methods are executed based on the order specified while inheriting the classes.

5. Key Learning Outcomes

By the end of the week, I could confidently:

  • Create and use classes in Python.

  • Reuse code through inheritance.

  • Apply polymorphism to give different behaviors to different objects.

  • Build my own iterators from scratch.

  • Understand how scope and MRO affect program behavior.

And most importantly, I could see how these concepts are not just theory — they apply to real-life cases like bank accounts, vehicles, or even file readers.