Class (Python)

A class is a blueprint for creating objects that bundle together data (attributes) and behavior (methods). You define one with the class keyword.

Python

Each object made from a class is called an instance. Classes are the foundation of object-oriented programming in Python.

Syntax

class Name:
    def __init__(self, ...):
        ...

Examples

Python

class Dog:
    def __init__(self, name):
        self.name = name
    def speak(self):
        return self.name + " says woof"

d = Dog("Rex")
print(d.speak())

Output:

Rex says woof

Learn it in a lesson

See also

Back to the Dictionary