Tools
Tools: Understanding Classes and Objects in Python
2026-02-02
0 views
admin
What Is a Class in Python? ## What Is an Object in Python? ## Built-in Classes in Python ## Literal Syntax in Python ## Modules and Built-in Classes ## Objects and Memory in Python ## Creating a Custom Class in Python ## Understanding main in Python Output ## Checking an Object’s Class with type() ## Summary 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. 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. An object is the actual house built from that blueprint. In programming, we call this an instance. You can think of it this way: 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. 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: 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. The parentheses () mean that an object of the class is being created
So: str() → object (instance) This is why we say class and instance (object) are closely related. 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. Every class in Python belongs to a 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. 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. This address shows where the object exists in memory. Now let’s move from built-in classes to custom classes. To create your own class in Python, you use the class keyword: 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: Just like one concept of humans, but many human beings. When you see output like: If the class were defined in another file, you would see the module name instead of __main__ You can check the class of any object using: 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). 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. Stay connected with hasabTech for more information: Website | Facebook | LinkedIn | YouTube | X (Twitter) | TikTok Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
print(type("Hello")) //Output: <class 'str'> Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
print(type("Hello")) //Output: <class 'str'> CODE_BLOCK:
print(type("Hello")) //Output: <class 'str'> CODE_BLOCK:
s0 = "" s1 = "Shameel" Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
s0 = "" s1 = "Shameel" CODE_BLOCK:
s0 = "" s1 = "Shameel" CODE_BLOCK:
s2 = "Shameel" Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
s2 = "Shameel" CODE_BLOCK:
s2 = "Shameel" CODE_BLOCK:
<__main__.User object at 0x106558c20> Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
<__main__.User object at 0x106558c20> CODE_BLOCK:
<__main__.User object at 0x106558c20> CODE_BLOCK:
class User: pass Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
class User: pass CODE_BLOCK:
class User: pass CODE_BLOCK:
u1 = User() Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
u1 = User() CODE_BLOCK:
u1 = User() CODE_BLOCK:
u2 = User() Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
u2 = User() CODE_BLOCK:
u2 = User() CODE_BLOCK:
<__main__.User object> Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
<__main__.User object> CODE_BLOCK:
<__main__.User object> CODE_BLOCK:
type(u1) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
<class '__main__.User'> Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
<class '__main__.User'> CODE_BLOCK:
<class '__main__.User'> CODE_BLOCK:
isinstance(s1, str) // True
isinstance(s1, int) // False
isinstance(u1, User) // True Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
isinstance(s1, str) // True
isinstance(s1, int) // False
isinstance(u1, User) // True CODE_BLOCK:
isinstance(s1, str) // True
isinstance(s1, int) // False
isinstance(u1, User) // True - 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. - Class = Blueprint
- Object = Instance created from that blueprint - 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) - The file you run is also considered a module
- Built-in classes like str belong to the builtins module - User is the class
- pass means the class is empty for now
To create an object from this class: - Multiple objects - __main__ is the current file being executed
- User is the class defined in that file - 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.
how-totutorialguidedev.toaipython