Python · live now

Custom classes

A class is a template for objects that each hold their own data. Write one with the class keyword, set an instance up with the __init__() method, and give every instance its own attributes and methods.

No account needed to read a lesson · 30-day money-back guarantee

See the full learning path

5

lessons

36

practice questions

81

prerequisite lessons

See it taught

1 of the 5 lessons here is free to preview - the whole explanation and worked example, with no account and no email.

The editor you get

A still of the editor, not a live one

Python Editor

1 2 3 4 5
class Dog: def speak(self): return "Woof" print(Dog().speak())

Editor Output

1

Output:

Woof

You write and run Python in the browser - nothing to install, no setup. Questions are marked on the values your code produces. The snippet here is an example of the kind of code you write.

The 5 lessons

The 5 lessons in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.

01

Introduction to object-oriented programming

9 questions

Learn how classes define the structure and behaviour of objects in Python, how to create independent instances from a class, and how each instance maintains its own separate state.

Covers

What is a class?

Creating an instance

Instances of a class are independent

02

Custom classes

6 questions

Learn how to define a custom class in Python using the class keyword and apply CamelCase naming conventions to ensure clarity and consistency in your code.

Covers

The class keyword and basic syntax

Naming conventions for classes

03

The __init__() method

Preview

9 questions

Learn how Python's __init__() method initialises new class instances, how to pass arguments to customise instance creation, and the role of self in giving access to the object being constructed.

Covers

What is __init__()?

Passing arguments to __init__()

The role of self

Preview this lesson - no account needed

04

Instance attributes

6 questions

Learn how to define instance attributes in Python classes by assigning values to self within the __init__() method, and how to access or modify these attributes for each object using dot notation.

Covers

Using arguments in __init__() to define attributes

Accessing and modifying attributes via dot notation

05

Instance methods

6 questions

Learn how to define and use instance methods in Python classes to operate on object data, including how to access attributes with self, pass additional arguments, return values, and call one method from another within the same class.

Covers

What is an instance method?

Working with instance methods

Where this unit sits in the curriculum

This unit is part of our Python Done Right learning path, which contains 100 lessons. Every one of them is drawn below.

The diagnostic test lets you skip any lesson you already know, including the ones in this unit.

Every dot is a lesson and every line a prerequisite. The 5 red dots are the lessons in Custom classes; the 81 blue dots feeding into them are the lessons Custom classes depends on; the 14 pale dots are the rest of the learning path, which come after Custom classes or alongside it.Custom classesFirst lessons on the left100 lessons

Hover any dot to name its lesson.

5 lessons in this unit

81 prerequisite lessons across the unit, counting every step back to the start

14 other lessons in the learning path - after this unit, or alongside it

Every dot is a lesson, every line a prerequisite. You can only start a lesson once you have mastered all of its prerequisites, so you are always building on solid foundations.

Start Custom classes

One subscription covers every learning path, and you can test out of anything you already know.

One subscription covers every learning path · 30-day money-back guarantee