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'>
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"
Here:
This is why we say class and instance (object) are closely related.
Literal Syntax in Python
When you write:
s2 = "Shameel"
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>
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
Here:
-
User is the class
-
pass means the class is empty for now
To create an object from this class:
u1 = User()
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()
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>
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)
Output:
<class '__main__.User'>
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
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