Skip to content

class

In Python, the class keyword lets you create user-defined classes, which are the cornerstone of object-oriented programming (OOP). With class, you can define the blueprint for objects, complete with attributes (data) and methods (functions).

Python class Keyword Examples

Here’s an example of how to define and use a class in Python:

Python
>>> class Dog:
...     def __init__(self, name):
...         self.name = name
...     def speak(self):
...         return f"{self.name} says woof!"
...

>>> dog = Dog("Frieda")
>>> dog.speak()
'Frieda says woof!'

In this example, we define a Dog class with an initializer method .__init__() to set the dog’s name. The .speak() method is an example of a behavior that objects of the Dog class can perform. Then, you create an instance of the Dog class named dog and call its .speak() method to see the output.

Tutorial

Python Classes: The Power of Object-Oriented Programming

Learn how to define and use Python classes to implement object-oriented programming. Dive into attributes, methods, inheritance, and more.

intermediate best-practices python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Jan. 7, 2025 • Reviewed by Dan Bader