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
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!
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!
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
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!
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)
One question:
Is
self.__size = "big"
private just because__
beforesize
? Would this work differently if we call this variable assize
?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.
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. :)
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 😯