DEV Community

Terry Threatt
Terry Threatt

Posted on

The Four Principles of Object Oriented Programming in Python

Object-oriented or OOP we shall call it is one of several different programming paradigms used in order to structure your code in a way that is easier to follow. It receives its name by defining objects you can interface within your Python programs.

Objects

Objects purposely represent real-world objects or things like a cat or dog. Python objects have a collection of related properties or behaviors like meow() or bark().

Classes

OOP in Python is class-based and your objects will be defined with the class keyword like the example below:

class Cat:
    pass 
Enter fullscreen mode Exit fullscreen mode

Principle 1 - Abstraction

Abstraction is the concept of hiding all the implementation of your class away from anything outside of the class.

class Dog:

    def __init__(self, name):
        self.name = name 
        print(self.name + " was adopted.")

    def bark(self):
        print("woof!")


# we don't care how it works just bark
spot = Dog("spot") #=> spot was adopted. 
spot.bark() #=> woof! 
Enter fullscreen mode Exit fullscreen mode

Principle 2 - Inheritance

Inheritance is the mechanism for creating a child class that can inherit behavior and properties from a parent(derived) class.

class Animal:

    def __init__(self, name):
        self.name = name 
        print(self.name + " was adopted.")

    def run(self):
        print("running!")


class Dog(Animal):

    def __init__(self):
        super().init 

    def bark(self):
        print("woof!")


# new dog behavior inherited from Animal parent class 
spot = Dog("spot") #=> spot was adopted. 
spot.run() #=> running! 
Enter fullscreen mode Exit fullscreen mode

Principle 3 - Encapsulation

Encapsulation is the method of keeping all the state, variables, and methods private unless declared to be public.

class Fish:

    def __init__(self):
        self.__size = "big"

    def get_size(self):
        print("I'm a " + self.__size + " fish")

    def set_size(self, new_size):
        self.__size = new_size 

# using the getter method
oscar = Fish()
oscar.get_size()  #=> I'm a big fish

# change the size 
bert = Fish()
bert.__size = "small" 
bert.get_size() #=> I'm a big fish

# using setter method
fin = Fish()
fin.set_size("tiny")
fin.get_size() #=> I'm a tiny fish
Enter fullscreen mode Exit fullscreen mode

Principle 4 - Polymorphism

Polymorphism is a way of interfacing with objects and receiving different forms or results.

class Animal:

    def __init__(self, name):
        self.name = name 
        print(self.name + " was adopted.")

    def run(self):
        print("running!")


class Turtle(Animal):

    def __init__(self):
        super().init 

    def run(self):
        print("running slowly!")


# we get back an interesting response 
tim = Turtle("tim") #=> tim was adopted. 
tim.run() #=> running slowly!
Enter fullscreen mode Exit fullscreen mode

Farewell

So now you a great deal about how object-oriented in Python works. If you enjoyed this post feel free to leave a comment about your experience with object-oriented programming.

Happy Coding,
Terry Threatt

Top comments (4)

Collapse
 
amnemonic profile image
Adam Mnemonic

One question:
Is self.__size = "big" private just because __ before size? Would this work differently if we call this variable as size?

Collapse
 
ykarthikeyan profile image
Karthikeyan Y

It is just a convention. It is understood that if we use __ in the beginning of a property name, it should be not be accessed from outside.

Collapse
 
amnemonic profile image
Adam Mnemonic

Yes, at the beginning of my journey with Python I also thought that it is only convention but visibility is also affected.
You can compare output of code using both versions:

When using self.__size (trinket) output is:
I'm a bigfish
I'm a bigfish
I'm a tinyfish

And running same code but using self.size (trinket) will give you output:
I'm a bigfish
I'm a smallfish
I'm a tinyfish

So, although code is technically correct, some explanation would be nice. :)

Thread Thread
 
nimai profile image
Nimai

Wow, I'm surprised you're the first person I've encountered who's talked about this. I just ran this code and it feels like a very important detail 😯