Introduction: Learn Python the Hard Way - Exercise 40.1

About: Interested in the intersection of hardware and software.

Zed Shaw's Learn Python The Hard Way is a great way to learn how to code. He teaches you how to code using proven teaching techniques such as building on what you know and repetition. I found his instructions easy to follow with minimal snags, until I encountered object-oriented programming on Exercise 40 (Modules, Classes, and Objects).

The goal of this Instructable is to follow Zed's teaching style to add one exercise between 40 & 41 in the Learn Python The Hard Way. Hopefully this will make it easier to learn object-oriented programming!

Step 1: Ex 40.1 Classes Are Photocopies

Once you've completed Exercise 40 in LPTHW, do this one next:

Object-oriented programming was invented to make code reusable. This takes the form of something called a Class. Here is the simplest version of a class.

Name this file ex40.1.py and type this in:

Line 1: class MyClass:

Line 2: def __init__(self):

Line 3: print "Hello Instructables!"

Line 4: x = MyClass()

This is the simplest way to write a class.

Line 1: This is the simplest way to write a class. Similar to defining a function, you type the word "class", hit the space bar, give the class a name, in this case, it's called "MyClass", and end it with a colon.

Line 2: "def__init__(self)" is just a function. You know this. The only difference is that it has this thing called "self". (We will talk about the "self" variable later.)

Line 3: When the __init__ function is called, it will run the print statement "Hello Instructables!"

Line 4: This creates a new object of MyClass and sets it equal to x. You learned this in the previous exercise, Exercise 40, and it's called instantiating. This is like making a photocopy of the class named MyClass, taking it out of the copier, and showing the copy to your friend. You had to name this photocopy x because that's the way computers work and they need to keep track of things.

The __init__ Function

Every time you make a photocopy (instantiating) of MyClass, which happens on Line 2 in the above code, that copy runs the __init__ function automatically. This is why "Hello Instructables!" appears on your computer screen when you run the code.

Self

A good way to think about this is how we use Post-it notes in the physical world: Each time you make a photocopy of a class, Python gives you a stack of post-it notes to stick on each photocopy. Post-it notes are a handy way to write notes and then stick those notes onto things. "Self" is the single post-it note you use on the photocopy. Each post-it note is unique to that one sheet of paper.

Change the code you typed in above to look like this:

class MyClass:

def __init__(self):

print "Hello Instructables!"

print self

x = MyClass()

Run the code. You should get this:

Hello World!

<__main__.MyClass instance at 0x01E98C88>

The second line is self, which is the object of the class.

Note: You will not have the exact same characters which follow "instance at....." Those set of numbers and letters are unique to your computer. But, everything before those weird characters should be the same.

Exercises

1. From memory, open a text editor and write code that displays the message "Hello Instructables!" using a class (just like above).

2. From memory, print the self variable.

3. Do the two exercises above each morning until you make no mistakes. You can do it.

Continue LPTHW with Exercise 41. Thanks for reading.