why does this hang

Robert Amesz reqhye72zux at mailexpire.com
Sun Aug 19 21:26:31 EDT 2001


Doug Fort wrote:

> John Hunter wrote:
> 
>> I have a class member function that returns a member of that
>> class, namely a date class that returns 'tomorrow' 
>> 
>> from Trade import MyDate
>> 
>> d = MyDate()
>> #mdy is month/day/year as a string
>> print 'today is %s' % d.get_mdy()
>> print 'tomorrow is %s' % (d.get_tomorrow().get_mdy())
>> 
>> 
>> If I don't put the parens around 'd.get_tomorrow().get_mdy()' the
>> code hangs (totally frozen, no response to Ctrl-C on a linux box)
>> 
>> Does the % operator bind more tightly than the '.' operator?  Is
>> this expected? 
>> 
> The formatted string is looking for a tuple or a dictionary
> following the '%'. It's not a case of '%' binding more tightly than
> '.'. By using the parentheses you are converting the output of the
> function to a tuple. I'm not too hip to Python internals, but I
> suspect the interpreter is trying to get a tuple from the address
> of the get_mdy() member, rather than calling the function. I like
> to use the form ... % (d.get_tomorrow().get_mdy(),) to make it
> explicit that it's a tuple. 

That's not entirely right: the % formatting operator only needs a tuple 
if there are multiple arguments to be formatted. And the % operator has 
a lower priority than the . and () operator, I'm virtually (99% +) 
certain of that.

So it's a bit of a mystery. The parentheses shouldn't make any 
difference whatsoever, they do not make the expression into a tuple, as 
you suggested, the comma operator does that.

So the bug must be in Python (egad!), or by some strange coincidence 
your code crashed when you tried it without the parentheses, but later 
(or earlier) it worked, perhaps because you tinkered with it, or it 
could be that the time was different and thus the data you worked with 
too. I assume MyDate() makes an object with the current date/time? 
There could be critical values (time periods) for which there's no 
get_tomorrow(), so to speak. ;-) 


Robert Amesz



More information about the Python-list mailing list