David Blaikie

David Blaikie has been an instructor of Python and Perl since 1999 at NYU School of Professional Studies, where he currently teaches Intro, Intermediate and Advanced Python. He also conducts Python training courses around the country, most recently at the Naval Surface Warfare Center in Dahlgren, VA. David recently published a Python Beyond the Basics training course through O'Reilly. He has been a Release Engineer at Google and DoubleClick, a software developer at Conde Nast and is currently a Python tech lead at AppNexus in New York.

Are there some students who can’t learn how to code?

Change tactics or give up: It's a crossroads many teachers face when students don't understand the code.

I can never forget an evening late into a semester of my Introduction to Python course, during which I asked my students a question about user-defined classes. Here’s the code I had put on the board:

class MyClass(object):

    var = 0

    def __init__(self):                # called 
        MyClass.var = MyClass.var + 1

x = MyClass()                          # new instance created
y = MyClass()                          # new instance created

As new information for this particular lesson, I informed them that every time a new MyClass instance is created, the __init__() method is called implicitly. In other words, the code above calls __init__() twice, and in executing the code in __init__(), the variable MyClass.var is being incremented — so this is also happening twice.

So, I asked them: after the above code is executed, what is the value of MyClass.var?

The hand of this class’ most enthusiastic student shot into the air.

“One!” He answered proudly. And for a moment my mouth stood open. Read more…