Class
In one line
A class is a blueprint for creating objects, defined with the class keyword, that bundles data (attributes) and behavior (methods).
In simple words
A class is a template or blueprint for creating objects. It defines what attributes (data) and methods (behavior) objects of that type will have. You define a class with the class keyword.
The __init__ method is the constructor — it runs when you create a new instance and initializes its attributes. Methods are functions defined inside the class; they take self as the first parameter to access the instance.
Classes support inheritance (a class can extend another), encapsulation (hiding internal state), and polymorphism (different classes can have methods with the same name). Classes are the foundation of object-oriented programming in Python.
Example
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return f"{self.name} says Woof!"
rex = Dog("Rex", 3)
print(rex.name) # Rex
print(rex.bark()) # Rex says Woof!Classes are defined with class, initialized with __init__, and methods use self to access instance data.
Common confusions
Confused with: object
The difference: A class is the blueprint/template. An object (instance) is a specific thing created from that class. A class defines the structure; an object is a concrete realization with its own data.
Related terms
Related Resources
Related Lessons
- Classes and Objects
Classes in Python.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.