DEV Community

Cover image for Beginner's guide to Object Oriented Programming in python(OOP)
DoreenNangira
DoreenNangira

Posted on

Beginner's guide to Object Oriented Programming in python(OOP)

Introduction

For most beginners, hearing the term Object-Oriented Programming for the first time can be intimidating. This is because majority look at it as a set of rules they have to follow instead of looking at it as a feature put in place to make their lives easier. In a nutshell, Object-Oriented Programming was introduced to enable us reuse various features of our code without having to write many lines. For those of us who have heard of the DRY(Don't Repeat Yourself) principle, OOP plays a crucial role in this.

What is Object-Oriented Programming?
This is a way of programming that involves putting together related behaviors and properties into individual objects.

Commonly used terms in OOP

- Class
A user defined data type. Just like we have built-in data types such as integer(int), OOP allows us to come up with our own data types. This enables us to define the properties and variables for our data types. To come up with a class, write the keyword class followed by the name of that class then a colon. The name of a class should always start with a capital letter and it is advised to stick to CamelCase naming convention. Check the example below:

class Human:
pass
Enter fullscreen mode Exit fullscreen mode

- Object
An item that has attributes and methods, also called an instance of a class. This means that an object is derived from a class. For example, the class Human can have objects such as x and y. Both x and y have the characteristics of the class Human such as having eyes, hair etc. The class Human in this case is called the blueprint of the objects x and y since it determines the characteristics an object should have.

Before an object is created, one has to make sure that the object gets the attributes defined by the class. This is called object instantiation. We use the init method to achieve this. The init method always has a default first parameter known as self. The self parameter serves as a reference to the class.

3 features of an object:
Attribute: Characteristics or distinguishing features of an object. For example, the class Human can have Height, Weight etc.
Method: The capabilities of an object. They are always represented in form of functions. For example, the class Human can have methods such as walk, swim etc.
Identity: A unique name given to an object.
The following example shows how we can go about creating our objects:

class Human:
 # attribute initialization
 def __init__(self, name):
  self.name = name
 # creating the methods
 def run(self):
  print("I run")
# instance creation of the class Human.
x = Human("Mary")
x.run()
print(x.run)
Enter fullscreen mode Exit fullscreen mode

In the above program, a class Human is created. We then introduce the attributes of the class with the init method. The attribute name is stored in a variable called name. The next step involves defining the capabilities of the class Human. We do this by use of methods, that is, a functional representation of the capabilities. For our case, our class is given the method run. The next step is creation of a class instance or an object. The object is called x. We then give the attributes of class Human to object x by assigning it to class Human followed by the attribute in parentheses. The object x gets the capabilities of class Human when we write x.run(). When we print the output of x.run(), we get I run. Good! You successfully created an instance of class Human!

We have come to the end of this article. However, this is only an introduction to OOP. I hope this will help you get started with OOP as it will act as a foundation for OOP. Stay tuned for more! Until then, Adios!!

Top comments (2)

Collapse
 
stefanmoore profile image
Stefan Moore

Thanks for the explaination. I was learning Python on my own 2 years ago and it all fell apart once I got to OOP. I'll pick this back up once I get into Boto3

Collapse
 
doreen970 profile image
DoreenNangira

Yes, I understand you @stefanmoore .OOP can be challenging to beginners but it is still possible to grasp it. I hope my article helped you and in case you have any questions just feel free to ask me.