parenting, programming

How a two year old helped me understand Object Oriented Programming

When I started learning programming, the hardest thing for me to understand was about classes and objects.   I had no idea how helpful it would be to watch a two year old narrate all his observations as he made sense of the world.  Our conversations would go something like this:

Kid: Look! A car!
Mommy: Yes, that’s a car.
Kid: That car Nissan.
Mommy: Yep, that’s a Nissan. How about the one in front of us?
Kid: That car Subaru.
Kid: That car has wheels.
Mommy: Yes, all cars have wheels.
Kid: That car has windshield.
Mommy: Yes, sweetie, all cars have windshields
Kid: That one hubcap fell off.
Mommmy: You’re right, that car is missing a hubcap.
Kid: That car go “beep beep”
Mommy: Yeah, we heard the car horn.

If I let it, this could go on for an hour.

But I realized what he was doing was creating multiple Car objects based off a Car class definition he was working out.

class Car(object):
    #There are some attributes that all cars have
    has_windshield = True
    has_wheels = True
    num_hubcaps = 4
    horn = "Beep beep"
    #And things that cars can do or make you do
    def all_hubcaps(self):
        if self.num_hubcaps == 4:
            print "Yes, that car has all its hubcaps."
        if self.num_hubcaps < 4:
            print "Oh, no, that car is missing a hubcap!"
    def make_noise(self):
        print("The car goes " + self.horn)

So now when he sees a car and recognizes it’s a Toyota, he creates a new instance of Car() in his mind, that inherits all the properties we gave Car().

>>>carA = Car()

And we can see that carA has a windshield, and wheels, and four hubcaps:

>>>carA.has_windshield
True
>>>carA.num_hubcaps
4

We can add in the car’s make, since we saw this particular one is a Toyota:
>>>carA.make = "Toyota"

Then we see another car, a Honda, that has only three hubcaps. Our class gave us something to say about that:

>>>carB = Car()
>>>carB.make = "Honda"
>>>carB.num_hubcaps = 3
>>>carB.all_hubcaps()
Oh, no, that car is missing a hubcap!

He was figuring out generally what a car is, and all unique identifying features of individual cars. He did the same with birds, as he saw pigeons and sparrows and robins. He did it with his clothes, identifying long sleeved shirts, short sleeved shirts, striped shirts, and so on. Programming becomes much less intimidating when you realize what you’re trying to build in code works just like a toddler trying to make sense of his world. He has an object oriented mind.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s