STEM with Python – Part 18: Python Fundamentals Ⅴ(Classes, Objects, and Inheritance)

Python’s OOP.

Since we’ve only been through some basic Python scripts and tools in the previous posts, it’s time to leap a bit more forward by mastering OOP.

So, this is the last part of Python fundamentals. At last, we’ll learn is one of the most yet fundamental ideas of the language – OOP.

Classes and Objects:

Since Python is another Object Oriented Language (OOP), we can hardly ignore classes and objects to fully master the language. Since we’ve only been through the very basics and some useful scripting tools written by Python previously, it’s time to jump a little further to re-skill ourselves to write even more powerful Python codes with OOP concepts.

Here’s the simplest form of Python OOP.

student.py

class Student:
    def __init__(self, name, major, gpa, is_on_probation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.is_on_probation = is_on_probation

student_main.py

from student import Student

student1 = Student("Jim", "Business", 3.1, False)
student2 = Student("Pam", "Art", 3.5, True)
print(student2.gpa)

Output:

3.5

Q&A App:

And this is a q&a app that takes advantage of Python OOP.

question.py

from questions_main import QuestionMain

question_prompts = [
    "What color are apples?\n(a) Red/Green\n(b) Purple\n(b) Orange\n\n",
    "What color are Bananas?\n(a) Teal\n(b) Magenta\n(b) Yellow\n\n",
    "What color are strawberries?\n(a) Yellow\n(b) Red\n(b) Blue\n\n"
]

questions_main = [
    QuestionMain(question_prompts[0], "a"),
    QuestionMain(question_prompts[1], "c"),
    QuestionMain(question_prompts[2], "b")
]

def run_test(questions_main):
    score = 0
    for question in questions_main:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
    print("You got" + str(score) + "/" + str(len(questions_main)) + " corresct")

run_test(questions_main)

questions_main.py

class QuestionMain:
    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

Output:

What color are apples?
(a) Red/Green
(b) Purple
(b) Orange

a
What color are Bananas?
(a) Teal
(b) Magenta
(b) Yellow

b
What color are strawberries?
(a) Yellow
(b) Red
(b) Blue

b
You got2/3 corresct

Class function:

This is an example of a Python class object accessing a class function.

student.py

class Student:
    def __init__(self, name, major, gpa, is_on_probation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.is_on_probation = is_on_probation

    def on_honor_roll(self):
        if self.gpa >= 3.5:
            return True
        else:
            return False

student_app.py

from student import Student

student1 = Student("James", "Programming", 3.5, False)
student2 = Student("Lisa", "Science", 3.3, True)

print(student1.on_honor_roll())

Output:

True

Inheritance:

If you’re already familiar with OOP, you might know inheritance. The power of inheritance is to implement the less-is-more philosophy.

TechPerson.py

class TechPerson:

    def do_program(self):
        print("Code in Java and C++!!")

    def do_linux(self):
        print("sudoooooo!!!!!")

    def do_fix_pc(self):
        print("I can fix pc!!")

TechPersonMain.py

from TechPerson import TechPerson
from TechPythonPerson import TechPythonPerson

myTech = TechPerson()
myTechPython = TechPythonPerson()

myTech.do_program()
myTech.do_fix_pc()
myTech.do_linux()

print("---------------")

myTechPython.do_program()
myTechPython.do_fix_pc()
myTechPython.do_linux()
myTechPython.do_program_in_python()

TechPythonPerson.py

from TechPerson import TechPerson

class TechPythonPerson(TechPerson):

    def do_program_in_python(self):
        print("I can program in Python")

Output:

Code in Java and C++!!
I can fix pc!!
sudoooooo!!!!!
---------------
Code in Java and C++!!
I can fix pc!!
sudoooooo!!!!!
I can program in Python

Leave a Reply