How To | Programming | Python

Intro to Object-oriented Programming in Python

OOP from the ground up with Examples from Pikachu and Bay Area Rapid Transit (BART)

Winston Robson
Future Vision
Published in
4 min readMay 16, 2019

What is Object-oriented Programming?

Object-oriented Programming (OOP), is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled together as individual objects. These objects contain data, in the form of fields (attributes), and code, in the form of procedures (methods).

Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.

Classes vs Objects (Instances)

When executing OOP with Python, each object is an instance of some class.

Think of a class as a blueprint for how an object may operate. The object is a particular instance derived from the class.

For example, an object could represent a particular person with attributes including name, age, address, etc., and methods like walking, talking, breathing, and running. The class would cover people in general.

Modified from The Code Ninja, check out their OOP article here

How to OOP in Python?

To kick off with Object-oriented Programming in Python, one first needs to construct a class.

Classes

Starting a class is as simple as class ThisIsAClass():.

Unlike a function, where one starts with def, writes in lowercase, and needs to end off with parentheses (e.g. def this_is_not_a_class():), a class utilizes capitalization and may or may not make use of parentheses.

I.e. classes can also be started like:

class ThisIsAClass:

class ThisIsAClass(object):

Attributes

From there, the next step is to initiate the class with an __init__() function. This is how attributes will be set, and brings in the self that is the object. It will look something like;

def __init__(self, name, object_type, health):

--

--

Winston Robson
Future Vision

Friend links: https://gumdropsteve.github.io/blog — “Energy may be likened to the bending of a crossbow, decision to the releasing of a trigger.”

Recommended from Medium

Lists

See more recommendations