What an object is and why you create them
An object in Python is a container that holds both data (called attributes) and actions (called methods). When you create an object, you are making a specific instance of a blueprint called a class. Think of a class as the instructions for a car — the object is the actual car you drive.
You create objects because they let you organize related information and actions together. Instead of keeping a person's name, age, and email address in three separate variables scattered through your code, you bundle them into one person object. This makes your code easier to read, easier to reuse, and easier to fix when something breaks.
Python comes with built-in objects you use without thinking about it — strings, lists, and numbers are all objects. But you will often need to create your own objects that match the specific problem you are solving.
Key Takeaways
- You define a class with the class keyword, then create an object from that class by calling the class name like a function.
- The __init__ method runs automatically when you create an object and is where you set up the object's starting data.
- Attributes store data inside an object, and you access them using a dot followed by the attribute name.
- Methods are actions an object can perform, and you call them the same way you access attributes — with a dot and parentheses.
- The word self inside a class always refers to the specific object you are working with.
Creating your first class and object
Start by defining a class using the class keyword. Here is the simplest possible example:
class Dog: pass
This creates an empty class called Dog. Now create an object from it:
my_dog = Dog()
The variable my_dog now holds a Dog object. The parentheses after Dog are what actually create the object — without them, you would just be referring to the class itself, not an instance of it.
This empty class is not very useful. The real power comes when you add data and actions to it. The next section shows you how.
Adding data to objects with attributes
Attributes are pieces of data that belong to an object. You define them inside a special method called __init__ (short for "initialize"). This method runs automatically every time you create a new object.
class Dog: def __init__(self, name, age): self.name = name self.age = age
The word self refers to the specific object you are creating. When you write self.name = name, you are saying "this object's name attribute should equal the name value that was passed in." Now when you create a Dog object, you must provide a name and age:
my_dog = Dog("Buddy", 3) your_dog = Dog("Max", 5)
Each object now has its own name and age. You access these attributes using a dot:
print(my_dog.name) # prints: Buddy print(your_dog.age) # prints: 5
Notice that my_dog.name and your_dog.name are separate — changing one does not change the other. Each object keeps its own copy of the data.
Adding actions to objects with methods
Methods are functions that belong to an object. They let you define actions the object can perform. Define a method inside the class just like you would define a function, but include self as the first parameter:
class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): print(self.name + " says woof!")
Now you can call the bark method on any Dog object:
my_dog.bark() # prints: Buddy says woof! your_dog.bark() # prints: Max says woof!
The method can use self to access the object's attributes. When you call my_dog.bark(), Python automatically passes my_dog as self inside the method, so the method knows which dog is barking.
Methods can also accept additional parameters beyond self. For example, you could add a method that takes a number:
def age_by_years(self, years): self.age = self.age + years
Then call it with an argument:
my_dog.age_by_years(2) # my_dog is now 5 years old
A complete example you can run
Here is a full working example that brings everything together:
class Person: def __init__(self, name, job): self.name = name self.job = job def introduce(self): print("Hi, I'm " + self.name + " and I work as a " + self.job) alice = Person("Alice", "engineer") bob = Person("Bob", "teacher") alice.introduce() # prints: Hi, I'm Alice and I work as a engineer bob.introduce() # prints: Hi, I'm Bob and I work as a teacher
Copy this code into a Python file and run it. You will see that each Person object remembers its own name and job, and each one can introduce itself independently.
Common mistakes when creating objects
The most common mistake is forgetting the parentheses when creating an object. Writing my_dog = Dog does not create an object — it just assigns the class itself to the variable. You need my_dog = Dog() with the parentheses.
Another frequent error is forgetting self as the first parameter in methods. Every method inside a class must have self as its first parameter, even if the method does not use it. Python uses self to know which object the method belongs to.
A third mistake is forgetting the dot when accessing attributes or calling methods. You cannot write my_dog name — you must write my_dog.name. The dot tells Python to look inside the object for that attribute or method.
Frequently Asked Questions
What is the difference between a class and an object?
A class is the blueprint or template. An object is a specific instance created from that blueprint. You define a class once, but you can create many objects from it. Think of a class as a cookie cutter and objects as the actual cookies you cut out.
Do I have to use __init__?
No, but you almost always will. The __init__ method is where you set up the object's starting state. If you do not define it, Python provides an empty one automatically. But without it, you have no way to give your objects their initial data.
Can an object have methods that do not use self?
Technically yes, but it is unusual. If a method does not need to access any attributes or other methods of the object, it probably should not be inside the class at all — it should be a regular function instead. Methods exist to let objects act on their own data.
What happens if I create two objects from the same class?
Each object is completely separate. They share the same methods (the same code), but each one has its own copy of the attributes. Changing one object's data does not affect the other object at all.
Can I change an object's attributes after I create it?
Yes. You can assign a new value to any attribute at any time using the dot notation: my_dog.age = 10. This changes only that specific object's attribute, not the class or any other objects.