Python Classes: The Basics

Introduction

Below is a conversation about python class basics. To help follow along, you may need to understand Python functions.

Anita: Hi DarkAnita. I’ve been trying to improve my Python skills by using Python classes, but it just looks so much like gibberish to me. Python functions, I totally understand those…but classes? Do you understand them?

DarkAnita: True, Python classes are not very intuitive at first glance. But if you understand each component, you will appreciate them even more. Let’s take a step back to define what Python is.

Anita: Python is an object-oriented programming language.

DarkAnita: Perfect. Since Python is an object-oriented programming language, it’s no surprise then that almost everything in Python is an object. This definition resonates even more when talking about Python classes.

Anita: How?

DarkAnita: It will make more sense once you know what a Python class is…

A Python class is a technique used to logically organize data and functions in ways that make them easy to reuse and extend in the future. Technically, it’s the blueprint for creating Python objects.

A Python object, on the other hand, is a particular instance of a class. In other words, an implementation or product of a class. For example, a class can be a Car blueprint, and the different cars built with the help of that blueprint design are instances of the class or objects.

Let’s consider the car analogy a bit further:

A car built from a blueprint has certain characteristics and functionalities. Some characteristics include the color of the car, its energy source, capacity, price, etc. And some functionalities include that it can move, be upgraded(or pimped), the value can change, etc.

Just like a car, a Python object has these two components, also known as attributes and methods.

An attribute refers to the characteristics of an object, while a method is more like the behavior or actions of an object. Characteristics are mostly nouns, while behaviors are generally verbs. When thinking about the components of a Python object, you can ask yourself questions like:

  • What does the object of the class have? i.e attributes
  • What can that object of the class do? i.e method

Anita: Ah I see. Just to be sure I understand what a Class is based on the Car Analogy;

In a Car factory, the car blueprint (which is the class) is to produce certain specifications of cars (which are the objects). Each car has a color, price, energy source, etc (which are the attributes), and each car can be upgraded and can have a price change ( which are the methods).

DarkAnita: Exactly.

Here is a visual representation of a class carblueprint that produces other cars:

In Python, a method is a Python function within a class. This is why a class is more of an organization of similar functions. With that in mind, this is what a typical class look like:

If you notice, there are 2 methods ( functions in a class) in the above example. What else do you notice?

Anita: I notice five things

  • The class command
  • The two functions or methods
  • The __init__ keyword
  • The self parameter
  • And the attribute 1 and 2 parameters

DarkAnita: Great observation

Let’s discuss each of these in more detail.

  • The class command/keyword is used to tell Python that you want to define a class or a blueprint. This class keyword must be followed by a class_name ( preferably a descriptive name). Conventionally, a class name uses a CamelCase format.
  • The __init__() method, also known as the initializer method, is used to initialize objects or help in building an object’s foundation. This instance method is where the object is built, along with all its attributes. This initializer method must be called __init__, and there’s a mandatory self argument this method takes. It is also the first argument and helps to differentiate and locate one object from another. The __init__() method is also the method that holds the attribute arguments and states the attributes of an object instantiated.

Examine this code snippet of a class—what are the attributes in this class?

Anita: Color, price, and size

DarkAnita: Excellent!!

Finally, let’s consider the second instance method, called second_method in the first example.

This looks like a typical function, but while inside a class, it’s a method or the behavior of an object. If you’ll notice, this method also has the self argument. It’s safe to say that all methods in a class should have the self argument as their first parameter.

You can have as many instance methods as you want in a class. However, make sure your class doesn’t become too clunky for readability purposes.

Now, let’s take a look at the complete class example. This time, you’re going to interpret what this class is doing:

Anita: This is a Shirt class that’s used to initialize or create shirt objects that have color, size, and price characteristics, and that have the behavior to change prices and have discounted prices.

DarkAnita: I see you have gotten the hang of Python classes…

Anita: Yeah, but what are the actual use cases for classes?

DarkAnita: Hmm…Good question. Some of the use cases are:

  • For organization: Classes are used for grouping functions together with similar purposes. When your code starts having a lot of functions, it may be time to define a class.
  • For reusability and readability — Since classes are a good way to keep similar functions together, it indirectly makes your code easy to read. Also, it makes it easier to modularize similar bits of code (i.e store code in modules or files) in order to be reused for another purpose.
  • To track info — Attributes are like the metadata of an object. Classes are great for storing that kind of information. Imagine you run a school and you want to keep track of all the data for students enrolled, as well as their behavioral records—you can define classes to track all this info.

Anita: This makes so much sense! I’m definitely going to add classes my Python toolkit.

Avatar photo

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

wix banner square