What a class is and why you need one

A class is a template in Python that lets you bundle related data and actions together. Instead of writing the same code over and over, you define it once in a class, then create as many copies of it as you need. Each copy is called an object or instance.

Think of a class like a blueprint for a house. The blueprint describes what rooms exist, where the doors go, and what the house can do. Each actual house built from that blueprint is an object — it has the same structure, but it's a separate, independent thing. You can paint one house blue and another red without affecting the blueprint or the other houses.

In practical terms, a class saves you time and makes your code easier to fix. If you need to change how something works, you change it in one place — the class definition — and every object created from that class automatically gets the update.

Key Takeaways

  • A class starts with the word class, followed by a name you choose, then a colon — for example, class Dog:
  • The __init__ method runs automatically when you create a new object and sets up its starting data.
  • Methods are actions the object can perform, written as functions inside the class and indented to show they belong to it.
  • You create an object by typing the class name followed by parentheses, like my_dog = Dog("Buddy", 5)
  • You access an object's data or run its methods using a dot, like my_dog.name or my_dog.bark()

The basic structure of a class

Every class starts the same way. Type class, then the name you want to give it (usually capitalized), then a colon. Everything indented under that line belongs to the class:

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

The __init__ method (those are double underscores on each side) is special — Python runs it automatically the moment you create a new object. It sets up the starting data for that object. The word self means "this specific object" — it lets you store information that belongs to each individual object, not to the class as a whole.

In the example above, when you create a Dog, you tell it the dog's name and age. The __init__ method takes those two pieces of information and stores them as self.name and self.age. Later, you can look up or change those values for any individual dog.

Adding methods — actions your object can perform

A method is a function that lives inside a class and describes something an object can do. You write it the same way you write a regular function, but it's indented to show it belongs to the class, and it always takes self as its first input:

class Dog:     def __init__(self, name, age):         self.name = name         self.age = age     def bark(self):         print(self.name + " says woof!")     def have_birthday(self):         self.age = self.age + 1         print(self.name + " is now " + str(self.age))

The bark method uses the dog's name (stored in self.name) to print a message. The have_birthday method increases the dog's age by one and prints a message. Both methods can see and change the object's data because they use self.

When you write a method, you always include self in the definition, but you never type it when you actually run the method. Python adds it automatically.

Creating objects from your class

Once you have written a class, you create an object by typing the class name followed by parentheses. Any information the __init__ method needs goes inside the parentheses:

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

Now you have two separate Dog objects. my_dog is a dog named Buddy who is 5 years old. your_dog is a dog named Max who is 3 years old. They are completely independent — changing one does not affect the other.

To see or use the data stored in an object, or to run one of its methods, use a dot between the object's name and what you want:

print(my_dog.name)                # prints: Buddy print(your_dog.age)                # prints: 3 my_dog.bark()                        # prints: Buddy says woof! your_dog.have_birthday()          # prints: Max is now 4

Each object keeps its own data. When you run your_dog.have_birthday(), only Max's age changes. Buddy stays 5.

A complete working example

Here is a full class you can type into Python and run right now:

class Car:     def __init__(self, brand, color, fuel):         self.brand = brand         self.color = color         self.fuel = fuel     def drive(self, distance):         self.fuel = self.fuel - distance         print(self.brand + " drove " + str(distance) + " miles")     def refuel(self):         self.fuel = 100         print(self.brand + " is now full") car1 = Car("Toyota", "blue", 50) car1.drive(10) print(car1.fuel)                # prints: 40 car1.refuel() print(car1.fuel)                # prints: 100

This Car class stores the brand, color, and fuel level for each car. The drive method uses up fuel based on how far you drive. The refuel method fills the tank back up. When you create car1 and run these methods, the car's fuel level changes and the methods print messages about what happened.

Common mistakes to avoid

The most common mistake is forgetting to indent the code inside the class. Python uses indentation to know what belongs inside the class and what is outside. If you do not indent, Python will give you an error. Use four spaces or one tab for each level of indentation, and be consistent.

Another mistake is forgetting self in the method definition or forgetting the dot when you use an object's data. If you write def bark(): instead of def bark(self):, Python will not know which object the method belongs to. If you write print(my_dog name) instead of print(my_dog.name), Python will not understand what you mean.

A third mistake is confusing the class itself with the objects you create from it. The class is the template. The objects are the actual things. You never run methods on the class — you run them on the objects. Dog.bark() will not work, but my_dog.bark() will.

Frequently Asked Questions

Do I have to use __init__, or can I skip it?

You do not have to write an __init__ method if your class does not need to store any starting data. However, most classes do use it. If you skip it, Python provides an empty one automatically, and you can still create objects — they just will not have any data stored in them.

Can a method take more than one input besides self?

Yes. A method can take as many inputs as you need. For example, def drive(self, distance, speed): takes two inputs in addition to self. When you run the method, you type them in order: my_car.drive(10, 50). The self is always first in the definition but never typed when you run it.

What is the difference between self.name and just name?

self.name stores data that belongs to each individual object and stays with that object. name (without self) is just a regular variable that only exists inside that one method and disappears when the method finishes. Use self when you want the data to stick around and be available to other methods.

Can I change an object's data after I create it?

Yes. You can change any data by using the dot notation: my_dog.age = 6 changes Buddy's age to 6. You can also add new data to an object the same way: my_dog.color = "brown" adds a color to Buddy even if the class did not originally store one.

What happens if I create two objects with the same name?

The second one replaces the first. If you type my_dog = Dog("Buddy", 5) and then my_dog = Dog("Max", 3), the first dog is gone and my_dog now refers only to Max. If you want to keep both, give them different names: dog1 = Dog("Buddy", 5) and dog2 = Dog("Max", 3).