Hone

Lessons · Python · classes (a blueprint)

A class is a blueprint; an object is one thing made from it

A class describes what a kind of thing has and does. Calling the class builds one object of that kind.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

A user, an order, a bank account, a game piece: real programs are full of things that carry their own data and their own actions. A class keeps the two together instead of scattering them across dicts and loose functions.

How to think about it

Ask: is this a KIND of thing that I will make many of? If yes, write a class for the kind and build objects from it. The class name is capitalised; the objects are ordinary variables.

Worked example

class Dog:
The blueprint. Nothing exists yet.
    pass
Empty for now; it still builds objects.
a = Dog()
Calling the class makes one Dog.
b = Dog()
A second, separate Dog.
print(a is b, type(a) is Dog)
False True: two objects, one class.

Your turn

Build one object from the class.

class Cat:
    pass

c = 

The trap

Writing Dog without the parentheses. Dog is the blueprint itself; Dog() is a built object, and only the object holds values.

Practise classes (a blueprint) on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.