[Quora] Explaining classes to a 10 year old

published 2020-04-11

Continuing my Quora answers series [1] with this question [2] which I answered on June 26, 2012:

How would you explain the concept of a "class" in Python to a 10 year old?

You cannot distinguish the concept of "class" from the concept of "instance". I think OOP is often taught the wrong way around, paradoxically because it is taught with languages that have support for class-based Object Oriented Programming such as Python (or Java for that matter). So excuse me but I will use another language (Lua, but you don't need to know to understand) to explain it. (Note: this will be bad Lua on purpose, but the point is not to teach Lua, it is to explain OOP).

Since you wrote "to a 10 year old" let's proceed with examples.

Imagine you come from another planet and you do not know what cats are. You encounter something small that purrs. You decide to name it Sam. Later on, you see something else very similar, except it is bigger, and you name it Max.

Let us describe Sam and Max in Lua.

sam = {
    name = "Sam",
    size = "small",
}

max = {
    name = "Max",
    size = "big",
}

Now we said that they purr, so let's define purring:

purr = function(self)
    print(self.name .. " purrs!")
end

purr(max)

Now you could stop here, or you could see purring as a property of Sam and Max. To represent that we could make `purr` a "method" of the "objects" Sam and Max:

sam.purr = purr

max.purr = purr

Now with the Lua syntax we cound also make Max purr like this:

max:purr()

Which is a short way to write this:

max.purr(max)

Since we said that `max.purr = purr` it works as expected.

After some time on Earth you realize there are lots of things like Sam and Max. Moreover there are lots of things Sam, Max and their friends do, such as sleep on keyboards. They also have things in common such as the fact they have two eyes.

You grow tired to say: "Max purrs. Max has two eyes. (...). Sam purrs. (...)". It would be much simpler to say give a name to the set of Sam, Max and their friends (for instance "Cats") and say "Cats purr. Cats have two eyes.".

Note that "Cats" is nothing physical, it is an idea, a category you have created for Sam, Max and their friends in order to be able to express things about them in an easier way.

Let's switch to Python to show how this is done now:

class Cat:

eyes = 2

def __init__(self, name, size):

    self.name = name

    self.size = size

def purr(self):

    print(self.name + " purrs!")

And how you use it:

sam = Cat(name="Sam", size="small")

sam.purr()

Proxy Information
Original URL
gemini://separateconcerns.com/2020-04-11-quora-class-concept.gmi
Status Code
Success (20)
Meta
text/gemini;lang=en_US
Capsule Response Time
164.861332 milliseconds
Gemini-to-HTML Time
0.406687 milliseconds

This content has been proxied by September (ba2dc).