Every object you work with in Python — a string, a list, a custom dog or bank account — is an instance. Understanding what that means is the key to understanding how Python thinks about data and behavior.
Python is an object-oriented language, which means it organizes data and logic into objects. An instance is simply one of those objects — a single, concrete thing that was created from a class. When you write my_list = [], Python creates an instance of the built-in list class and assigns it to my_list. When you define your own class and call it, you get back an instance of that class. This tutorial explains what instances are, how they are created, how they store data, and how to interact with them.
What Is an Instance?
A class is a blueprint. An instance is the actual object built from that blueprint. Think of a class as a cookie cutter and each instance as one of the cookies it stamps out. Every cookie has the same shape — defined by the cutter — but each one is a separate, physical object you can decorate differently.
In Python, when you call a class, you are telling Python to build a new object that follows that class's blueprint. The object that comes back is an instance. Here is the simplest possible example:
class Dog:
pass
rex = Dog() # rex is now an instance of Dog
luna = Dog() # luna is a separate instance of Dog
print(rex) # <__main__.Dog object at 0x...>
print(luna) # <__main__.Dog object at 0x...> — different address
rex and luna are two distinct objects. Even though both came from the same Dog class, they live at different memory addresses and are completely independent of each other. Changing one does not affect the other.
The memory address shown when you print an instance (the 0x... part) will be different every time you run your program. It is just Python telling you where in memory that specific instance lives.
You can verify that an object is an instance of a particular class using isinstance():
print(isinstance(rex, Dog)) # True
print(isinstance(luna, Dog)) # True
print(isinstance(rex, list)) # False
Everything in Python — integers, strings, lists, functions — is an instance of some class. When you type "hello", Python creates an instance of the built-in str class. When you type [1, 2, 3], Python creates an instance of list. Custom classes work exactly the same way.
Build the line that checks whether rex is an instance of the Dog class:
isinstance(obj, ClassName) takes the object first and the class second, separated by a comma. The call must be wrapped in parentheses. type and is serve different purposes and are not the right tool here.
Classes and Instances Working Together
A class with pass is not very useful. In practice, classes define an __init__ method that runs automatically every time a new instance is created. Inside __init__, you attach data to the instance using self.
self is simply a reference to the instance that is being created right now. When Python calls __init__, it automatically passes the new instance as the first argument. By convention, everyone names that parameter self, though Python itself does not care what you call it.
class Dog:
def __init__(self, name, age):
self.name = name # attach name to this instance
self.age = age # attach age to this instance
rex = Dog("Rex", 4)
luna = Dog("Luna", 2)
print(rex.name) # Rex
print(luna.name) # Luna
print(rex.age) # 4
When you call Dog("Rex", 4), Python creates a blank instance, then calls __init__ with that instance as self, "Rex" as name, and 4 as age. The two lines self.name = name and self.age = age attach those values to the instance. The finished instance is then returned and stored in rex.
You do not need to pass self when calling a method. Python passes it automatically. Only write self in the method definition itself, never in the call.
Here is a comparison of how three different instances from the same class each hold their own, independent data:
- rex.name
- "Rex" — stored on this instance only
- rex.age
- 4 — stored on this instance only
- luna.name
- "Luna" — stored on this instance only
- luna.age
- 2 — stored on this instance only
- biscuit.name
- "Biscuit" — stored on this instance only
- biscuit.age
- 7 — stored on this instance only
This Dog class has one bug that will raise a TypeError when you try to create an instance. Click the line you think is wrong, then hit check.
rex = Dog("Rex", 4). The __init__ method requires both name and age arguments. Calling Dog("Rex") only provides one, so Python raises a TypeError: __init__() missing 1 required positional argument: 'age'.
Instance Attributes and Instance Methods
Data stored on an instance using self is called an instance attribute. A function defined inside a class that takes self as its first parameter is called an instance method. Together, instance attributes and instance methods make up the core of how objects work in Python.
Instance attributes hold an object's state — its current data. Instance methods define what an object can do — its behavior. Here is a more complete example showing both:
class Dog:
def __init__(self, name, age):
self.name = name # instance attribute
self.age = age # instance attribute
self.tricks = [] # instance attribute — starts empty
def learn_trick(self, trick): # instance method
self.tricks.append(trick)
def describe(self): # instance method
return f"{self.name} is {self.age} years old."
rex = Dog("Rex", 4)
rex.learn_trick("sit")
rex.learn_trick("shake")
print(rex.describe()) # Rex is 4 years old.
print(rex.tricks) # ['sit', 'shake']
luna = Dog("Luna", 2)
print(luna.tricks) # [] — Luna's tricks list is separate from Rex's
Notice that rex.tricks and luna.tricks are separate lists, even though they were both created by the same line self.tricks = [] in __init__. That is because each time you create an instance, a brand-new list is created for that specific object. The two instances share the same blueprint but own their own copies of the data.
A common mistake is to define a mutable default like a list at the class level instead of inside __init__. If you write tricks = [] at class level, all instances share the same list. Changes made by one instance affect every other instance. Always initialize mutable attributes inside __init__ using self.
You can also modify instance attributes directly from outside the class using dot notation:
rex.age = 5 # update the age attribute directly
print(rex.age) # 5
print(luna.age) # 2 — luna is unaffected
"Objects are Python's main abstraction for data." — Python Data Model, Python Software Foundation Documentation
How to Create and Use Instances in Python
Follow these five steps each time you want to build a class and work with its instances. The steps apply whether you are writing a simple script or a larger application.
-
Define a class
Use the
classkeyword followed by your class name and a colon. Class names follow the CapWords convention — for example,Dog,BankAccount, orUserProfile. Inside the class body, add an__init__method that acceptsselfand any other data the instance needs at creation time. -
Set instance attributes inside __init__
Inside
__init__, writeself.attribute_name = valuefor each piece of data the instance should hold. These assignments run every time a new instance is created, so each instance gets its own separate copy of the data. -
Add instance methods
Write functions inside the class body with
selfas their first parameter. These methods can read the instance's attributes withself.attribute_nameand modify them withself.attribute_name = new_value. Methods define what an instance can do. -
Create an instance by calling the class
Call the class name like a function:
my_obj = MyClass(arg1, arg2). Pass any arguments that__init__requires (not includingself— Python handles that). The returned value is your new instance, ready to use. -
Access attributes and call methods using dot notation
Read an attribute with
my_obj.attribute_name. Update it withmy_obj.attribute_name = new_value. Call a method withmy_obj.method_name(). Python automatically passes the instance asself— you never include it in the call.
Python Learning Summary Points
- A class is a blueprint. An instance is a concrete object built from that blueprint. You can create as many instances from a class as you need, and each one is independent.
__init__runs automatically when a new instance is created. Use it to set instance attributes withself.attribute_name = value. Every instance gets its own copy of those attributes.- Instance methods take
selfas their first parameter and use dot notation to access and modify the instance's attributes. When you call a method, you never passself— Python passes it for you. - Everything in Python is an instance of some class. Strings, lists, integers, and your own custom objects all follow the same instance model.
- Use
isinstance(obj, ClassName)to check whether an object is an instance of a particular class. This is safer than comparing types directly withtype().
Instances are the building blocks of object-oriented Python. Once you understand that every object is an instance of a class — with its own attributes and access to the class's methods — you have the foundation needed to work with dataclasses, inheritance, Python's standard library, and third-party frameworks like Django or SQLAlchemy.
Frequently Asked Questions
An instance is a specific object created from a class. When you call a class like a function, Python creates a new instance — an independent object that has its own copy of the attributes defined in that class.
A class is a blueprint or template that defines what attributes and methods an object will have. An instance is an actual object built from that blueprint. You can create many instances from a single class, and each one is independent.
__init__ is a special method called automatically when a new instance is created. It lets you set up the instance's initial attribute values. The first parameter, self, always refers to the instance being created.
self is the first parameter of every instance method. It refers to the specific instance the method is being called on. Python passes it automatically — you do not include it when calling the method yourself.
Yes. Each instance stores its own attribute values independently. If you create two Dog instances, one named Rex and one named Luna, changing Rex's name attribute does not affect Luna's name attribute.
An instance attribute is a variable that belongs to a specific instance. It is typically set inside __init__ using self.attribute_name = value. Each instance has its own copy and can store different values.
An instance method is a function defined inside a class that takes self as its first parameter. It can read and modify the instance's attributes. You call it on a specific instance using dot notation: my_object.method_name().
You create an instance by calling the class name followed by parentheses, passing any required arguments. For example: my_dog = Dog('Rex', 3). Python calls __init__ with those arguments and returns the new instance.
isinstance(obj, ClassName) returns True if obj is an instance of ClassName (or a subclass of it), and False otherwise. It is a safe way to check what type of object you are working with before calling its methods.
If no other variable holds a reference to the instance, Python's garbage collector eventually removes it from memory. As long as at least one variable references the instance, it stays alive in memory.