Skip to main content

Command Palette

Search for a command to run...

Oop In Python

Published
•3 min read•View as Markdown
Oop In Python
E

Currently learning and developing my skills in data science.

Table of Contents

  1. Introduction

  2. What I learned

  3. What I built

  4. Challenges and solutions

  5. Conclusion

Introduction

OOP stands for object-oriented programming; it allows us to structure our code into classes and objects for reusability and better organization.

Classes define what an object should look like. Imagine a scenario where you have multiple cars, different brands or models, but they all can drive around. They are blueprints that show what objects can do.

An object is created based on that class; imagine you have a bunch of toys, for example, blocks, dolls, and cars. A block can be used to build a tower, a doll can be dressed up, and a car can be driven around. Objects are the toys in this scenario that do special things.

class Students:
    x = 10
print(Students)
student1 = Students()
print(student1.x)

Advantages of OOP

  • Makes code easier to debug, maintain and reuse

  • Allows you to build reusable applications with less code.

  • Flexibility, which makes code more adaptable to changes and easier to extend with new features without significant modifications to existing code.

All classes have a method called __init__() method which is executed when a class is initiated. It is used mainly to assign values to object properties.

class Students:
   def __init__(self,name,age):
        self.name = name
        self.age = age

student1 = Students("Sandra", 16)
print(student1.name)

If, for some reason, a class definition has no content. Pass statement is used to avoid getting errors.

class Person:
    pass

What I learned:

  • Key concepts: Inheritance, Iterators, Polymorphism and Scope

  • Frameworks: Pure python OOP

  • Techniques mastered: Defining classes and using __init__ constructors, Inheritance (code reuse & specialization), Polymorphism (method overriding), Iterators (__iter__), Global scope and Local scope.

Inheritance

allows a class to inherit all the methods and properties from another class.

Parent class: is also called base class; it is the class being inherited from.

Child class: also known as derived or subclasses. It is the class that inherits from another class.

#example of base class
class Students:
   def __init__(self,name,age):
        self.name = name
        self.age = age
    def printname(self):
    print(self.name, self.age)

student1 = Students("Sandra", 16)
student1.printname()

#example of child class
class Student(Students):
  pass

Iterators

Are object that contains a countable number of values.They implements the iterator protocol consisting of __iter__() and __next__(). They are used to perform an action on each item in a given set.

Note: Lists, tuples, dictionaries and sets are iterable objects. Iterators allow for processing large datasets without loading the entire sequence into the computer’s memory.

class CountDown:
    def __init__(self, n):
        self.n = n
        self.number = n

    def __iter__(self):
        return self

    def __next__(self):
        if self.number <= 0:
             raise StopIteration
        x = self.number
        self.number -= 1
        return x

countdown = CountDown(15)
for num in countdown:
    print(num)

Polymorphism

Poly means many. Polymorphism refers to methods/functions with the same name that can be executed on many classes.

class Car:
    def __init__(self,brand,model):
        self.brand = brand
        self.model = model
    def move(self):
        print("Car drives")
class Bicycle:
    def __init__(self,brand,model):
        self.brand = brand
        self.model = model
    def move(self):
        print("Bicycle pedals")
car1 = Car("Ford","Mustang")
bicycle1 = Bicycle("Ibiza","Touring 2")

for x in (car1,bicycle1):
    print(x.brand)
    print(x.model)
    x.move()

Scope

Local scope: variable created inside a function and can only be accessed inside that function.

def myfunc():
  x = 300
  print(x)

myfunc()

Global Scope: can be available within any scope.

x = 300
def myfunc():
  x = 200
myfunc()
print(x)

Challenges & Solutions:

  • Challenge: The correct arguments placements when classes inherits from other classes.

Solution: Reflect on the error message, go over the previous class, trace arguments.

Conclusion

Learning these concepts helped me actually see how OOP isn’t just theory, it’s structure, it gave me confidence that I can design programs that are not only functional but also organized, reusable, and scalable.

More from this blog

My Python Learning Journey

10 posts