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.