What Is a Class in Python?
A class in Python is a blueprint for creating objects.
If that sounds abstract, think of it this way:
- A class describes what something has
- A class also describes what something can do
- The actual thing you create from that description is called an object
Classes help you group data and behavior in one place. In Python, the data is usually stored in attributes, and the actions are defined with methods.
Simple example of a class
Here is a small working example that shows a class, an object, an attribute, and a method:
class Dog:
species = "animal"
def __init__(self, name):
self.name = name
def bark(self):
print(self.name + " says woof")
my_dog = Dog("Max")
print(my_dog.name)
my_dog.bark()
Output:
Max
Max says woof
What this code does:
class Dog:creates a class namedDog__init__sets up each new dog with anameself.nameis an attributebark()is a methodmy_dog = Dog("Max")creates an object from the class
If you are new to this topic, it helps to learn what an object is in Python next, because classes and objects are closely connected.
What a class means
A class is a template for making objects.
It lets you put related data and related actions together. This makes code easier to read and organize.
For example, a Dog class might contain:
- attributes like
nameandage - methods like
bark()andrun()
In Python:
- data inside a class is usually called attributes
- functions inside a class are called methods
So instead of storing everything in separate variables and functions, a class keeps related pieces together.
Why classes are useful
Classes are useful because they help you organize code.
They are especially helpful when:
- you want to create many similar objects
- the same kind of data and actions belong together
- your program is getting large enough that structure matters
For beginners, it is often easiest to think of a class as a way to model a real thing.
Examples:
- a
Dog - a
Student - a
BankAccount - a
Car
Each of these has data and actions. A student might have a name and grade. A bank account might have a balance and a method to deposit money.
If you want a larger beginner lesson, see Python classes and objects explained.
Class vs object
This is one of the most important ideas to understand.
- A class is the blueprint
- An object is one real item made from that blueprint
Example:
Dogis the classMaxis an object created fromDog
You can create many objects from one class.
class Dog:
def __init__(self, name):
self.name = name
dog1 = Dog("Max")
dog2 = Dog("Bella")
print(dog1.name)
print(dog2.name)
Output:
Max
Bella
Both dog1 and dog2 come from the same class, but they store different data.
Basic parts of a class
Here are the main parts beginners should know.
class keyword
The class keyword starts a class definition.
class Dog:
pass
This creates a class named Dog.
__init__
The __init__ method sets up starting values for each object.
class Dog:
def __init__(self, name):
self.name = name
When you create a new Dog, Python runs __init__ automatically.
If this part feels confusing, see the __init__ method in Python explained.
self
self refers to the current object.
It tells Python which object's data you are working with.
class Dog:
def __init__(self, name):
self.name = name
Here, self.name means “the name of this specific dog.”
Attributes
Attributes store values on an object.
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
In this example, name and age are attributes.
Methods
Methods are functions inside a class.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(self.name + " says woof")
Here, bark() is a method because it belongs to the Dog class.
You may also want to read what a method is in Python.
When beginners should use a class
Use a class when several values and actions belong together.
Good beginner examples include:
DogStudentBankAccountCar
A class is often a good choice when you find yourself thinking:
- “These values belong to one thing”
- “I want many similar items”
- “Each item should have its own data”
Do not force classes into every program.
For very small scripts, simple variables and functions are often enough. You do not need classes just because Python supports them.
If you want to understand the bigger idea behind classes, see object-oriented programming in Python explained.
Common beginner confusion
Here are some common points that confuse beginners.
- A class is not the same as a function
- A class creates objects, while a function usually performs one task
selfmust appear in instance methods- Defining a class does nothing by itself until you create an object
For example, this defines a class but does not create any object:
class Dog:
def bark(self):
print("Woof")
To use it, you still need to create an object:
my_dog = Dog()
my_dog.bark()
Common mistakes
Beginners often run into the same problems when learning classes.
Mixing up a class and an object
A class is the definition. An object is the thing you create from it.
class Dog:
pass
my_dog = Dog()
Here:
Dogis the classmy_dogis the object
Forgetting to create an object before using attributes or methods
This will not work:
class Dog:
def bark(self):
print("Woof")
Dog.bark()
bark() is an instance method, so it needs an object.
Correct version:
class Dog:
def bark(self):
print("Woof")
my_dog = Dog()
my_dog.bark()
Not understanding what self refers to
self means the current object.
If you leave it out, your method definition will be wrong.
Wrong:
class Dog:
def bark():
print("Woof")
Correct:
class Dog:
def bark(self):
print("Woof")
Thinking classes are required for every Python program
They are not.
Many beginner programs work perfectly well with:
- variables
ifstatements- loops
- functions
Classes become useful when your code needs more structure.
Helpful ways to inspect a class or object
If you are exploring how classes work, these commands can help:
print(type(my_dog))
print(my_dog.__dict__)
print(Dog.__dict__.keys())
help(Dog)
What they show:
type(my_dog)shows the type of the objectmy_dog.__dict__shows the object's attributesDog.__dict__.keys()shows names defined on the classhelp(Dog)shows built-in help information
Example:
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Max")
print(type(my_dog))
print(my_dog.__dict__)
Possible output:
<class '__main__.Dog'>
{'name': 'Max'}
FAQ
What is a class in Python in simple words?
A class is a blueprint for creating objects that store data and perform actions.
What is the difference between a class and an object?
A class is the definition. An object is one instance created from that definition.
Do beginners need classes right away?
No. Beginners can learn variables, functions, and loops first. Classes become useful when code needs better structure.
What is self in a Python class?
self refers to the current object, so Python knows which object's data to use.
See also
- Python classes and objects explained
- Object-oriented programming in Python explained
- The
__init__method in Python explained - What is an object in Python?
- What is a method in Python?
If you understand the basic definition of a class, the next step is to learn how to create your own classes and objects step by step.