Objects are how Python stores related data and actions together

An object in Python is a container that holds both information (called attributes) and actions (called methods) in one place. You create an object by first defining a class — a blueprint that says what data and actions your object will have — and then making an instance of that class, which is the actual object you use in your code.

The simplest way to create an object is to define a class with the class keyword, add a special method called __init__ that sets up the object's starting data, and then call the class name like a function to make an instance. Python handles the rest.

Key Takeaways

  • A class is a blueprint; an instance is the actual object you create from that blueprint.
  • The __init__ method runs automatically when you create an object and sets up its starting attributes.
  • You create an object by typing the class name followed by parentheses, like my_dog = Dog().
  • Methods are functions that belong to an object and can access that object's data using self.
  • Attributes are pieces of data stored inside an object, set either in __init__ or added later.

Define a class and add the __init__ method

Start by writing a class definition. Use the keyword class, give your class a name (usually starting with a capital letter), and add a colon. Inside the class, define the __init__ method, which Python calls automatically when you create a new object.

The __init__ method always takes self as its first parameter. self refers to the specific object being created and lets you store data inside it. After self, add any other parameters you want to pass when creating the object.

Here is a concrete example:

class Dog:     def __init__(self, name, age):         self.name = name         self.age = age

This class is named Dog. When you create a Dog object, you pass in a name and an age. The __init__ method stores those values as attributes of the object using self.name and self.age.

Create an instance by calling the class name

Once you have defined a class, you create an object (an instance) by typing the class name followed by parentheses and any arguments the __init__ method expects. Assign the result to a variable so you can use the object later.

Using the Dog class from above:

my_dog = Dog("Buddy", 3) another_dog = Dog("Max", 5)

Now my_dog is an object with name set to "Buddy" and age set to 3. another_dog is a separate object with different data. Each object is independent — changing one does not affect the other.

Access and modify an object's attributes

Once you have created an object, you can read its attributes by typing the object name, a dot, and the attribute name. You can also change an attribute by assigning a new value to it.

print(my_dog.name) # prints "Buddy" print(my_dog.age) # prints 3 my_dog.age = 4 # change the age print(my_dog.age) # prints 4

You can also add new attributes to an object even if they were not set in __init__. For example, my_dog.color = "brown" creates a new attribute called color on the my_dog object.

Add methods to make your objects do things

Methods are functions that belong to an object. They let your object perform actions and often use the object's own attributes. Define a method inside the class just like you define __init__, with self as the first parameter.

class Dog:     def __init__(self, name, age):         self.name = name         self.age = age     def bark(self):         print(f"{self.name} says woof!")

Now you can call the bark method on any Dog object:

my_dog.bark() # prints "Buddy says woof!"

The method uses self.name to access the object's own data. When you call my_dog.bark(), Python automatically passes my_dog as self, so the method knows which object it belongs to.

Use __init__ to set up complex starting data

The __init__ method can do more than just store simple values. You can create lists, dictionaries, or other objects inside __init__ to give your object more structure from the start.

class Dog:     def __init__(self, name, age):         self.name = name         self.age = age         self.tricks = [] # start with an empty list     def learn_trick(self, trick):         self.tricks.append(trick)         print(f"{self.name} learned {trick}!")

my_dog = Dog("Buddy", 3) my_dog.learn_trick("sit") my_dog.learn_trick("stay") print(my_dog.tricks) # prints ["sit", "stay"]

This pattern is useful when you want each object to maintain its own collection of data that changes over time.

Frequently Asked Questions

What is the difference between a class and an object?

A class is a template or blueprint. An object is an actual instance created from that template. Think of a class as a recipe and an object as a cake made from that recipe. You define a class once, but you can create many objects from it.

Do I have to use __init__?

No, but it is the standard way to set up an object's starting data. If you do not define __init__, Python provides an empty one automatically. Most real code uses __init__ because it makes your objects more predictable and easier to use.

Can I create an object without passing any arguments?

Yes. If your __init__ method only takes self (no other parameters), you can create an object by just typing the class name and parentheses: my_object = MyClass(). You can also make parameters optional by giving them default values in the method definition.

What does self actually do?

self is a reference to the specific object being worked with. When you call a method on an object, Python automatically passes that object as self. This lets the method access and modify that object's data without confusion when multiple objects of the same class exist.

Can I delete an attribute from an object?

Yes, using the del keyword: del my_dog.age removes the age attribute from my_dog. After that, trying to access my_dog.age will cause an error. This is rarely needed in practice.