Master Object-Oriented Programming in Python

Part 3: Implementing OOP

Christian Yurianja
Python in Plain English

--

After designing the diagram, the last thing that you should do is make it into a code.

As I've explained in my previous article, designing a class diagram will help you or other developers identify the program that you gonna make. It will be a lot easier if you have some “blueprint” that people can easily read and understand. Let me bring you again the diagram, so we can see it while I explain the code.

From the above diagram, the code that I’ve created is like this:

class Publication:
def __init__(self, title, price):
self.title = title
self.price = price


class Periodical(Publication):
def __init__(self, title, price, publisher, period):
super().__init__(title, price)
self.period = period
self.publisher = publisher


class Book(Publication):
def __init__(self, title, author, pages, price):
super().__init__(title, price)
self.author = author
self.pages = pages

def showBook(self):
print(f"{self.title} book is written by {self.author}. "
f"The total pages is {self.pages} and priced at ${self.price}")


class Magazine(Periodical):
def __init__(self, title, publisher, price, period):
super().__init__(title, price, publisher, period)


class Newspaper(Periodical):
def __init__(self, title, publisher, price, period):
super().__init__(title, price, publisher, period)


b1 = Book("Rich Dad Poor Dad", "Robert T. Kiyosaki", 336, 10)
n1 = Newspaper("The Wall Street Journal", "Dow Jones & Company", 1.0, "Daily")
m1 = Magazine("Forbes", "Forbes Inc", 14.99, "Monthly")

b1.showBook()
print(b1.author)
print(n1.publisher)
print(b1.price, m1.price, n1.price)

You see, Publication is the only class that is not inherited from another class. If you see on the code, we define methods called __init__ . But actually, we don’t create a new method. We just use the implicit method from class, then change it as we want. This is called overriding. The __init__ function itself will always run first, even if you don’t declare it.

class Publication:
def __init__(self, title, price):
self.title = title
self.price = price

The next class is called Periodical. It is inherited from Publication class, shown by Publication text that we placed inside the parentheses and the superclass code super().__init__(title, price) we use under the __init__ method.

You’ll notice that, inside the __init__ parameter, there are four parameters which are title, price, publisher, and period. This is the value that you should input when calling the Periodical function.

While on the super().__init__(title, price) code, there are only 2 parameters which are title and price. This means we only inherited title and price variables from its superclass. Then, we can define our own variable for this class. In this case, we define variables period and publisher.

class Periodical(Publication):
def __init__(self, title, price, publisher, period):
super().__init__(title, price)
self.period = period
self.publisher = publisher

In the third class, there is a Book class that is inherited from the Publication class. As you can see below code, we define other methods named showBook. This is the new method that we created exclusively for the Book class. You can define any function here.

class Book(Publication):
def __init__(self, title, author, pages, price):
super().__init__(title, price)
self.author = author
self.pages = pages

def showBook(self):
print(f"{self.title} book is written by {self.author}. "
f"The total pages is {self.pages} and priced at ${self.price}")

On showBook function, I define the code that can print messages from data we input.

For Magazine and Newspaper class, it is inherited from Periodical class. There is no additional variable or method that we modified.

class Magazine(Periodical):
def __init__(self, title, publisher, price, period):
super().__init__(title, price, publisher, period)


class Newspaper(Periodical):
def __init__(self, title, publisher, price, period):
super().__init__(title, price, publisher, period)

After defining the class, let's see how we use it. I assign b1 for Book class, n1 for Newspaper class, and m1 for Magazine class with arguments in it.

Then I use the b1.showBook() command to print the b1 value. Also respectively I print the author on b1, publisher on n1, and price of b1, m1, and n1.

b1 = Book("Rich Dad Poor Dad", "Robert T. Kiyosaki", 336, 10)
n1 = Newspaper("The Wall Street Journal", "Dow Jones & Company", 1.0, "Daily")
m1 = Magazine("Forbes", "Forbes Inc", 14.99, "Monthly")

b1.showBook()
print(b1.author)
print(n1.publisher)
print(b1.price, m1.price, n1.price)

When I run the code, the result will be like this:

As you can see, if we design our code to be object-oriented, the code to call function is structured well and easy to understand. We can also protect the variable from changing outside of the class. This way, if you work with a lot of data it will be easier to maintain for future development.

That’s all my article about Master Object-Oriented Programming in Python. It is a good starting, when we think about object-oriented from the start of the project, even before starting to write your code. In the next project, I’ll use an object-oriented way to code my program so it’s more manageable.

So, see you in the next articles…

All Mastering Object-Oriented Programming in Python series:

  1. Master Object-Oriented Programming in Python — Part 1: What is OOP?
  2. Master Object-Oriented Programming in Python — Part 2: Designing Class Hierarchy
  3. Mastering Object-Oriented Programming in Python — Part 3: Implementing OOP!

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

--

--

9K+ Views on Medium || I am a Technology and Software Developer. Contact me via Upwork here : https://s.id/chrisanja