oop

A simple approach to understanding classes and objects with python .

A new programmers’ dilemma in object oriented programming (OOP)

Delving deeper into programming requires learning new paradigms like object oriented programming, but for a new programmer who is just getting solid on conditionals, iterations and other forms of structured programming may find OOP a little bit difficult to understand. Well some may understand but breaking it down for a non-technical person may be tasking.

Well, when discussing OOP words like objects, classes, encapsulation, etc are often heard, but for this article I am going to stick to the concept of objects and classes.

OBJECTS

Objects in simple terms are a representation of real world elements like cars, dogs, chairs and the like. Objects share two main characteristics: data and behavior.

Lets use this analogy, take a Dog for example, it has a variety of data like breed, size, age, colour etcetera and behaviours like run, sleep, eat, bark.

In OOP, data are referred to as attributes or instances and behavior as methods. Methods are simply functions that call these objects in our code.

Objects(instances/attributes) Methods(behaviours)

Breed….eat()

Size ….. sleep()

Age…… bark()

Colour …..run()

CLASSES

A class is a blueprint from which individual objects are created. It can be seen as a model or a way to define attributes and behavior of an element. A class sort of houses all objects and methods

Example: class Dogs:

pass

maltese = Dogs()

print maltese

Here maltese is an object (instance) of the class Dogs.

Say we want to build a Dog management software.This would entail modeling real life entities.

i.e Dogs into software objects.

Lets go!

Dogs (attributes)

Breed

Size

Colour

Age

Methods (behaviours)

Eat()

Sleep()

Bark()

Run()

objects

Dog1

breed = alsetian

size = small

age = 2 years

colour = black

Dog2 :

breed = maltese

size = medium

age = 3 years

colour = white

Dog3

breed = chow chow

size = large

age = 5 years

colour = brown

So thus far, we have created objects from our data for three dogs. So lets code this in python. Yay!

Class Dogs:

def __init__(self, breed, size, age, colour)

self.breed =breed

self.size =size

self.age =age

self.colour =colour

def dogData_self(self):

print(“Dog details are: Dog breed- ” + breed + “Dog size: ” + size + “Dog age:” + age + “Dog colour: ” + colour)

Dog1 = Dogs(“alsetian”, ”small”, str(2)+”years”, ”black”)

Dog2 = Dogs(“maltese”, ”medium”, str(3)+”years”, ”white”)

Dog3 = Dogs(“chow chow ”,”large”, str(5)+”years”, ”brown ”)

Dog1.dogData_self()

The code outputs:

Dog details are: Dog breed — alsetian Dog size: small Dog age: 2years Dog colour: black

So here’s a breakdown of the code above:

So on the first line we created a class called Dogs.

On the second line we defined a method with a constructor init. Which helps create several instances or objects in this case were breed, size, age ,and colour. ”self” is a general convention used in python to initialize objects, so that when they are called the value assigned to them is returned.

On lines 3 to 6 we assigned the objects that would be used to call them.

On lines 7 to 9 we created a function that prints out the breed, size , age and colours of the dogs.

On lines 10 to 12 we give values to the objects i.e like creating a variable.

On lines 13 to 15 we called the methods (functions) this will return the values stored in them .

So there it is easy-peasy right! Well , more practice helps commit this to memory. So get out your keyboards and get your hands dirty with some code!

--

--

--

UX Designer | Front-end web developer | Tech community builder | Budding Digital artist .

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Princess Oviawe

Princess Oviawe

UX Designer | Front-end web developer | Tech community builder | Budding Digital artist .

More from Medium

ThreadPoolExecutor in Python3

Python vs C++: Key Difference Between Them

What is Variables?

How to cache a method of an unhashable type in Python?