Object-Oriented Programming in Python Part 1: Creating a Class

Image for post
Image for post

Overview

What Is Object-Oriented Programming (OOP)?

Image for post
Image for post

Here we have an example of a class that creates a Dog object. It might look intimidating being new or unfamiliar to OOP, so let’s break things down.

Class

A class is essentially used when a category of objects have similar properties and attributes, but the objects differentiate from others by having different types of these properties and attributes.

In essence, a class is a blueprint for creating objects that have the exact behaviour, but different types of attributes and properties.

Object

Instance

Self

Note: self does not have to be named self and you could call it whatever you desire, although it is conventionally called self and it is best to keep it that way. Self is always the first parameter of any method in a class

__init__

Image for post
Image for post

Alright, so now that we understand a few key concepts, let’s take a walkthrough as to what is happening in this image.

Here, we are creating an object of a Dog. The attributes a dog has in this class are:

  1. The dog has a name (a string).
  2. The dog has an age (a float, if the dog is less than a year, its age can be represented as a decimal).
  3. The dog has a breed (a string).
  4. The dog has a weight (an integer).

Under the declaration of __init__ you see:

Image for post
Image for post

Essentially what we are doing here is initializing the attributes of an object. This step is important since different dogs can have different names, ages, breeds and weights, which is why we must do self.attribute = attribute.

Whenever we are referencing a specific attribute of an object, we must always put self.attribute.

Image for post
Image for post

Now moving onto the next part of the Dog class, we see a method called bark. When we call bark on an object of Dog, it will return “the name of the dog (self.name) goes bark!” We are using self.name because different objects of Dog will have different names, so by using self.name, we are referring to the specific name of that instance.

Notice, in def bark(self), we see that self is the only parameter within the function, and every function in a class must pass in self, because we are referring to a specific instance of a class.

It is worth noting that if you do not return a value in a function, it will return the value none, which is why I did return string, rather than print(string).

Image for post
Image for post
Output if you were to do print(f’{self.name} goes bark!) rather than returning the value.
Image for post
Image for post

The next method within this class is called ispuppy, and this method will tell the user if the dog is a puppy or not a puppy. A puppy is defined if the Dog object’s age is less than or equal to one, otherwise, the Dog is not considered a puppy.

In the if statement, we are putting self.age, to reference the specific age of the instance of the object.

Image for post
Image for post

The last method within this class is called size. Based on the weight of an object’s instance, the function will return if the dog is small, medium, or big.

Image for post
Image for post

Lastly, we need to create an actual instance of the object Dog. I have called this object charlie, and here are its attributes.

  1. self.name = “Charlie”
  2. self.age = 1
  3. self.breed = Poodle
  4. self.weight = 30

Note, when you create an object of a class, all the parameters of __init__ must be provided, unless you set an attribute to None or False. Additionally, parameters must be in the order provided in __init__.

Attributes can also be predefined for example:

Image for post
Image for post

This is the same thing as creating charlie = Dog(‘Charlie’, 1, ‘Poodle, 30), but instead, you are defining the attributes within the __init__ method in the class. If you were to create the object after the class is created, but you also defined your attributes within the __init__ , different attributes can be overwritten declaring your object after the class is created.

Okay, so now that we have created charlie, which is an instance of Dog, we can call different methods on charlie.

First, let’s see if we can get the output to return the different attributes charlie has in __init__.

If we do:

Image for post
Image for post

our output will actually be:

Image for post
Image for post

Essentially, printing the object charlie, will result in an output that returns the memory address of charlie (different objects are located in different memory addresses).

Although, if we want to access different attributes of an object, this can be accomplished by the print object.attrbute.

Let’s try accessing charlie’s name:

Image for post
Image for post

Our terminal will now successfully output:

Image for post
Image for post

This can be done with any attribute that belongs to an object.

We can also call other methods on our object charlie.

Let’s try calling the object bark on charlie.

Image for post
Image for post

Note, since bark is a function, it must be called with parenthesis, otherwise, the output of the function will be the memory address of where it is located at.

Here is the output:

Image for post
Image for post

Now that we have successfully called our first method, let’s try calling the second one, ispuppy.

Image for post
Image for post

Here is the output:

Image for post
Image for post

Since we initialized that the instance charlie has an age of one, our function will return that it is a puppy. Although, if the instance’s age were to be greater than one, the output would return that charlie is not a puppy.

Lastly, now that we have successfully called our first two functions, we can proceed to call the final method on charlie, which is called size.

Image for post
Image for post

Here is the output:

Image for post
Image for post

Since the object charlie has a weight of 30, the output will return that Charlie is a small dog.

I highly encourage you to play around with this Dog class, by creating more instances and changing the different values of the output.

To practice making simple classes, here are a few ideas you can try:

  1. Class for different shapes (can calculate the perimeter or area of a shape)
  2. Classes for other animals
  3. Classes for people (a normal person, an employee at a company, etc.)
  4. A calculator class that can return different options (adding, subtracting, etc.)

Contact me for any inquiries 🚀

If you would like to recommend topics that you would like to see me write about (programming, A.I., or other), if you would like more resources, or have any general inquires, you can contact me by:

💫Email: ashleycinquires@gmail.com

💫 Linkedin

Links To Other Articles in This Series 🚀

Innovator and AI enthusiast

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