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

DoanVietTrungAtGmail doanviettrung at gmail.com
Tue Jan 15 04:54:34 CET 2013


Yes you are right, it was printed. I think my eyes are tired. I left the
PC, came back, and saw your reply plus the printed line. Thanks Mitya, and
I apologise to everyone about false alarm! The answer of 42 was staring at
me and I didn't see it.

Trung

On Tue, Jan 15, 2013 at 2:33 PM, Mitya Sirenef <msirenef at lightbird.net>wrote:

> 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<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/
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130115/10414b56/attachment.html>


More information about the Tutor mailing list