[Tutor] TypeError: not enough arguments for format string

broek at cc.umanitoba.ca broek at cc.umanitoba.ca
Wed Jul 2 21:11:58 CEST 2008



----- Message from bgailer at gmail.com ---------
     Date: Wed, 02 Jul 2008 14:32:27 -0400
     From: bob gailer <bgailer at gmail.com>

> broek at cc.umanitoba.ca wrote:
>> [snip]
>
>>
>> In a case like
>>
>>>>> print '%s%s' %42 , 333
>> ...
>> TypeError: not enough arguments for format string
>>
>> it would be ambiguous whether 333 was intended as a second   
>> formatting argument or as a second thing to be printed.
>
> Here's where the Language Reference comes in handy:
>
> print_stmt ::= "print" ( [expression ("," expression)* [","]]
> expression_list ::= expression ( "," expression )* [","] # An
> expression list containing at least one comma yields a tuple
>
> Given that, there is no ambiguity in print '%s%s' %42 , 333
>


I think I poorly expressed my intent. It was my starting point that
42, 333
provides a tuple, even absent the parens.

What I meant to express was the hypothesis that
>>> print '%s%s' %42 , 333
even while following the '%' with a tuple might be ruled out due to a  
possible 'ambiguity in the intent', not in the syntax. I meant to  
suggest that the perhaps the parens were required to make it explicit  
where the end of the '%'-following tuple was, and where the 'more  
stuff to print, distinct from the format-string stuff' began.

Were the following legal:
>>> c = "I am distinct from the format string, but that is hard to see."
>>> print "%s%s" % 42, 333, c
>> ...
>> TypeError: not enough arguments for format string

it would be harder to parse than:
>>> c = "I am obviously distinct from the format string."
>>> print "%s%s" % (42, 333), c
42333 I am obviously distinct from the format string.


If not that, then why are the parens required?

Best,

Brian vdB


More information about the Tutor mailing list