DEV Community

Shameel Uddin
Shameel Uddin

Posted on • Originally published at blog.hasab.tech

Understanding Classes and Objects in Python

Now it’s time to clearly understand what a class is and what an object is in Object-Oriented Programming (OOP) using Python.

These two concepts are the foundation of OOP, and once you understand them, everything else becomes much easier.

What Is a Class in Python?

In simple words, a class is a blueprint or a template for creating objects that share common properties.

To understand this, let’s use a real-world example.

As discussed in our previous blog, first the concept of a human being was created, and then you and I were created from that concept. That concept is like a class, and our individual existence is like an object.

In Python terms:

  • A class defines the structure (attributes) and behavior (methods).
  • It describes what an object will look like
  • It doesn’t occupy memory by itself because it’s just a concept. So basically, a class is just a blueprint, not a real thing.

What Is an Object in Python?

An object is the actual house built from that blueprint. In programming, we call this an instance.

You can think of it this way:

  • Class = Blueprint
  • Object = Instance created from that blueprint

In Python, you’ll hear people use "object" and "instance" interchangeably. Don't let that confuse you they refer to the same thing: the real, usable version of a class.

Built-in Classes in Python

You might be surprised to learn that you’ve been using classes since Day 1. Every data type in Python—strings, integers, lists, and dictionaries is actually a class.

Try running this in your terminal:

           print(type("Hello"))
          //Output: <class 'str'>
Enter fullscreen mode Exit fullscreen mode

See that? str is a class provided by Python. When you create a string like name = "Shameel", you are actually creating an object of the str class.

            s0 = ""
            s1 = "Shameel"
Enter fullscreen mode Exit fullscreen mode

Here:

  • str is the class
  • str() creates an object
  • The parentheses () mean that an object of the class is being created
    So:

  • str → blueprint

  • str() → object (instance)

This is why we say class and instance (object) are closely related.

Literal Syntax in Python

When you write:

s2 = "Shameel"
Enter fullscreen mode Exit fullscreen mode

This is called shorthand literal syntax.

Behind the scenes, Python still creates an object of the strclass. Python just hides this complexity to make the language easy and beginner-friendly.

Modules and Built-in Classes

Every class in Python belongs to a module.

  • The file you run is also considered a module
  • Built-in classes like str belong to the builtins module

That’s why when you inspect a string class, you’ll see it comes from builtins, even though you didn’t import anything manually.

Objects and Memory in Python

When you create an object, Python carves out a little piece of your computer's RAM to store it. Each object gets a unique "home address."

If you print a custom object, you’ll often see something like this: <__main__.User object at 0x106558c20>

That long code at the end (0x1065...) is the physical memory address where that specific object lives.

<__main__.User object at 0x106558c20>

Enter fullscreen mode Exit fullscreen mode

This address shows where the object exists in memory.

Creating a Custom Class in Python

Now let’s move from built-in classes to custom classes.

To create your own class in Python, you use the class keyword:

class User:
    pass
Enter fullscreen mode Exit fullscreen mode

Here:

  • User is the class
  • pass means the class is empty for now To create an object from this class:
u1 = User()
Enter fullscreen mode Exit fullscreen mode

The () tells Python to create an object from the User class and store it in the variable u1.

You can create multiple objects from the same class:

u2 = User()
Enter fullscreen mode Exit fullscreen mode

This means:

  • One class
  • Multiple objects

Just like one concept of humans, but many human beings.

Understanding main in Python Output

When you see output like:

<__main__.User object>
Enter fullscreen mode Exit fullscreen mode

It means:

  • __main__ is the current file being executed
  • User is the class defined in that file

If the class were defined in another file, you would see the module name instead of __main__

Checking an Object’s Class with type()

You can check the class of any object using:

type(u1)
Enter fullscreen mode Exit fullscreen mode

Output:

<class '__main__.User'>
Enter fullscreen mode Exit fullscreen mode

This confirms that u1 is an object of the User class.
Checking Object Instances with isinstance()
This is a great way to "double-check" an object. In programming, any function starting with "is" usually returns a True or False (Boolean).

Examples:

isinstance(s1, str)   // True
isinstance(s1, int)   // False
isinstance(u1, User)  // True
Enter fullscreen mode Exit fullscreen mode

In programming, when a sentence starts with “Is”, the result is usually a Boolean value (True or False).

This method is widely used in Python to validate object types.

Summary

  • Object-Oriented Programming (OOP) in Python is built around classes and objects.
  • A class is a blueprint that defines structure and behavior.
  • An object (instance) is a real entity created from a class.
  • Python’s built-in data types like str, int, list, and dict are actually classes.
  • Objects are created using parentheses () and stored in memory.
  • You can create your own custom classes using the class keyword.
  • Use type() to check an object’s class.
  • Use isinstance() to verify whether an object belongs to a specific class.
  • Understanding classes and objects is essential for mastering Python OOP.

Stay connected with hasabTech for more information:

Website | Facebook | LinkedIn | YouTube | X (Twitter) | TikTok

Top comments (0)