Demystifying Python Classes for Beginners 🐍

Kishore Jalleda
Python in Plain English
4 min readNov 6, 2023

--

Ever wondered what ‘classes’ are in Python programming? Think of them as blueprints for building something useful — like houses in a neighborhood.

🏠 Classes: The Blueprint

Imagine you’re an architect. You’ve got a blueprint for a house. This blueprint is a ‘class’ in Python. It outlines the structure of what you’re going to build but isn’t a house by itself. Instead, it tells you where the walls, windows, and doors should go; or what the color of the house should be; etc.

class House:

def __init__(self, color):
self.color = color

def open_door(self):
print("Welcome in!")

In this simple class, House, we’ve got a special function (known as a method) called __init__, which sets up our house when we build it, and another method open_door, which represents an action our house can perform.

🔑 Objects: The Real Deal

When you actually build a house from the blueprint, that’s an ‘object’ in Python. You can build many houses from the same blueprint, but each one can be a different color or have different furniture. Each house (object) you build is an instance of your House class.

my_house = House(color="blue")
your_house = House(color="red")

Here, my_house and your_house are objects or instances of the House class, each with a different color.

🚪 Methods: Actions Your Object Can Perform

Just like how you can open the door of your house, you can perform actions with objects in Python using methods. For our House class, we can ‘open the door’ like this:

my_house.open_door()  # Prints "Welcome in!"

This tells my_house to perform the open_door method — in real life, it would mean opening the actual door of your blue house.

🛋️ Attributes: Characteristics of Your Object

Attributes are like the characteristics or properties of your object. Our house’s color is an attribute — some houses are blue, some are red, and so on. You can access these directly:

print(my_house.color)  # Prints "blue"

🌟 Python’s self: A Key to Understanding Classes 🗝️

In our blueprint analogy, self is like saying “this particular house” whenever you talk about it. It’s a way for each house to refer to itself, its own color, or its own doors.

When we defined the open_door method inside our House class, we wrote it like this:

def open_door(self):
print("Welcome in!")

Here, self represents the instance of the House that’s performing the action.

So when my_house opens the door, self is my_house; when your_house opens the door, self is your_house.

It’s a bit like saying “I” when you’re talking about yourself. So, when my_house uses self.color, it’s talking about its own color.

This might sound a bit confusing at first, but once you start using it, it should become second nature — just like understanding that “I” refers to yourself when you’re speaking!

So, self is the way a Python class refers to the individual object’s own properties and actions. It’s how an object says “my own” in the world of Python classes. 🐍

Dive into coding with this understanding, and you’ll see how self helps you keep track of each object’s unique qualities in your code creations.

Ready to speak ‘Python’ fluently? Start by practicing with self and see the difference it makes!

🔧 But…Why Use Classes?

Classes help you organize and reuse code.

Once you have a blueprint, you can make many houses without drawing each one from scratch. In coding, this means once you’ve defined what a House is and what it can do, you can create as many House objects as you need, each with its own settings.

So there you have it, classes in Python are not so different from blueprints in the real world. They help us create objects with properties (like color) and behaviors (like opening a door) that we can interact with in our code.

Ready to build your own ‘houses’ in Python? Give it a try and see what amazing structures you can construct!

📍Oh, One last thing…

if you are interested in contributing to open-source, do give my project UnStruct.AI (on GitHub) a try. The core app is in python and hope this beginner-friendly tutorial can help with getting you started.

Also, check out this super-simple tutorial on the understanding of Pull Requests and how can submit your first one:

A Beginner-Friendly Guide to Pull Requests (PR).

Hope to see you there!

Kishore Jalleda

#Python #Programming #CodingForBeginners #LearnToCode #opensource #community

In Plain English

Thank you for being a part of our community! Before you go:

--

--