[Tutor] Why doesn't line n execute before line n+1?

DoanVietTrungAtGmail doanviettrung at gmail.com
Tue Jan 15 04:14:24 CET 2013


Dear tutors

In learning about the __call__ magic method, in the code below I
deliberately omitted __call__ and, as expected, I got the error message
"TypeError: 'Test' object is not callable". But I am surprised that the
print statement was not executed, even though the interpreter sees it
first. Why is that?

I thought that the Python interpreter executes line by line. That is, in
the code below,:
-First, it executes the class definition because these 2 lines are what it
sees first
-Second, it creates an instance of the class Test, called test
-Third, it executes the print statement
-Only then would it encounter the error of calling the instance as if it
were callable

class Test(object):
    pass
test  = Test()
print "I am puzzled. Why isn't this line printed?"
test()

Making the puzzle worse for me, when I tried adding another print statement
before the test = Test() line, the interpreter behaved as I expected!

Trung
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130115/9362d4e7/attachment.html>


More information about the Tutor mailing list