Skip to content
Justice Tech School

Python You Need: Classes

Django models, views and forms are classes. A class groups data and methods, and inheritance reuses code. This pure Python example runs.

Example
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} makes a sound"

class Dog(Animal):
    def speak(self):
        return f"{self.name} barks"

print(Animal("Generic").speak())
print(Dog("Rex").speak())

Result

Generic makes a sound
Rex barks

Exercise

Inherit from Animal.

class Dog():

Quiz Test

What is self?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.