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

Mitya Sirenef msirenef at lightbird.net
Tue Jan 15 04:33:03 CET 2013


On Mon 14 Jan 2013 10:14:24 PM EST, DoanVietTrungAtGmail wrote:
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

It does print the line for both in 2.7 and 3.3.  - mitya


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/


More information about the Tutor mailing list