class

A blueprint for objects that bundle data (attributes) and actions (methods).

Syntax

class Name:
def __init__(self, value):
self.value = value
  • self is the object the method was called on.

Example

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

print(Robot("Pip").name)

Output:

Pip

Common mistakes

  • Forgetting self. in self.name = name stores the value in a variable that disappears.

Practise it