<div class="gmail_quote">On Wed, Nov 16, 2011 at 4:04 PM, Steven D&#39;Aprano <span dir="ltr">&lt;<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">Wayne Werner wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span style="background-color: transparent; ">&lt;snip&gt; It was explained to me once that in</span></blockquote></div>

<div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
this case:<br>
<br>
&quot;%s&quot; % 42<br>
<br>
That since python expects to see a single-element tuple it treats it as or<br>
converts 42 to a single element tuple.<br>
</blockquote>
<br></div>
&quot;Treats as&quot; may be true; &quot;converts to&quot; not so much. What it actually does is this:<br>
<br>
py&gt; import dis<br>
py&gt; dis.dis(compile(&#39;&quot;%s&quot; % x&#39;, &#39;&#39;, &#39;single&#39;))<br>
  1           0 LOAD_CONST               0 (&#39;%s&#39;)<br>
              3 LOAD_NAME                0 (x)<br>
              6 BINARY_MODULO<br>
              7 PRINT_EXPR<br>
              8 LOAD_CONST               1 (None)<br>
             11 RETURN_VALUE<br>
<br>
<br>
Notice that the call to BINARY_MODULO (the % operator) takes two arguments, the string &quot;%s&quot; and the object x, whatever it happens to be. Python can&#39;t convert x to a tuple at this point, because it doesn&#39;t know what x is, and it may not know how many format specifiers are in the string either.<br>


<br>
Once the string and the object hit BINARY_MODULO, all bets are off. It will do whatever it likes, because that&#39;s purely internal implementation.</blockquote><div><br></div><div>Ah, very cool. Just because I was interested, I did the same thing, only using (x,) and there was only one difference (line? 6):</div>

<div><br></div><div><span style="background-color: transparent; ">&gt;&gt;&gt; dis.dis(compile(&#39;&quot;%s&quot; % (x, )&#39;, &#39;&#39;, &#39;single&#39;))</span></div><div>  1           0 LOAD_CONST               0 (&#39;%s&#39;)</div>

<div>              3 LOAD_NAME                0 (x)</div><div>              6 BUILD_TUPLE              1</div><div>              9 BINARY_MODULO</div><div>             10 PRINT_EXPR</div><div>             11 LOAD_CONST               1 (None)</div>

<div>             14 RETURN_VALUE</div><div><br></div><div>&lt;PSA tune&gt;</div><div>The more you know!</div><div><br></div><div>Thanks,</div><div>-Wayne</div></div>