codegurukool.com · Free coding tutorials & online compiler

Home › Python Tutorial › Python classes and objects

Python classes and objects

Bundle data and behaviour.

A class is a blueprint. __init__ runs when an object is created, and self is the object itself. Methods are functions inside the class.

Example code

class Dog:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} says woof"

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

Run this code in the online editor

← Python number formattingPython inheritance and polymorphism →