I believe i encountered repr()in the Python tutorial, but i had not kept the relevance of it in my memory..<br><br><div class="gmail_quote">On Mon, Feb 22, 2010 at 9:35 PM, Sithembewena Lloyd Dube <span dir="ltr">&lt;<a href="mailto:zebra05@gmail.com">zebra05@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">@spr, thanks for the explanation, especially on representations of strings. To think that i freely used repr(variable_x) without fully understanding the meaning and the power of that function..<div>
<div></div><div class="h5"><br><br><br><br><div class="gmail_quote">
On Mon, Feb 22, 2010 at 9:37 AM, spir <span dir="ltr">&lt;<a href="mailto:denis.spir@free.fr" target="_blank">denis.spir@free.fr</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Just a little complement to Steven&#39;s excellent explanation:<br>
<br>
On Mon, 22 Feb 2010 10:01:06 +1100<br>
Steven D&#39;Aprano &lt;<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>&gt; wrote:<br>
<br>
[...]<br>
<div><br>
&gt; So if you write a pathname like this:<br>
&gt;<br>
&gt; &gt;&gt;&gt; path = &#39;C:\datafile.txt&#39;<br>
&gt; &gt;&gt;&gt; print path<br>
&gt; C:\datafile.txt<br>
&gt; &gt;&gt;&gt; len(path)<br>
&gt; 15<br>
&gt;<br>
&gt; It *seems* to work, because \d is left as backlash-d. But then you do<br>
&gt; this, and wonder why you can&#39;t open the file:<br>
<br>
</div>I consider this misleading, since it can only confuse newcomers. Maybe &quot;lonely&quot; single backslashes (not forming a &quot;code&quot; with following character(s)) should be invalid. Meaning literal backslashes would always be doubled (in plain, non-raw, strings). What do you think?<br>


<div><br>
&gt; But if the escape is not a special character:<br>
&gt;<br>
&gt; &gt;&gt;&gt; s = &#39;abc\dz&#39;  # nothing special<br>
&gt; &gt;&gt;&gt; print s<br>
&gt; abc\dz<br>
&gt; &gt;&gt;&gt; print repr(s)<br>
&gt; &#39;abc\\dz&#39;<br>
&gt; &gt;&gt;&gt; len(s)<br>
&gt; 6<br>
&gt;<br>
&gt; The double backslash is part of the *display* of the string, like the<br>
&gt; quotation marks, and not part of the string itself. The string itself<br>
&gt; only has a single backslash and no quote marks.<br>
<br>
</div>This &quot;display&quot; is commonly called &quot;representation&quot;, thus the name of the function repr(). It is a string representation *for the programmer* only, both on input and output:<br>
* to allow one writing, in code itself, string literal constants containing special characters, in a practical manner (eg file pathes/names)<br>
* to allow one checking the actual content of string values, at testing time<br>
<br>
The so-called interactive interpreter outputs representations by default. An extreme case:<br>
&gt;&gt;&gt; s = &quot;\\&quot;<br>
&gt;&gt;&gt; s<br>
&#39;\\&#39;<br>
&gt;&gt;&gt; print s, len(s)<br>
\ 1<br>
&gt;&gt;&gt; print repr(s), len(repr(s))<br>
&#39;\\&#39; 4<br>
&gt;&gt;&gt;<br>
The string holds 1 char; its representation (also a string, indeed) holds 4.<br>
<div><br>
&gt; The best advice is to remember that Windows allows both forward and<br>
&gt; backwards slashes as the path separator, and just write all your paths<br>
&gt; using the forward slash:<br>
&gt;<br>
&gt; &#39;C:/directory/&#39;<br>
&gt; &#39;C:textfile.txt&#39;<br>
<br>
</div>Another solution is to take the habit to always escape &#39;\&#39; by doubling it.<br>
<br>
<br>
Denis<br>
________________________________<br>
<br>
la vita e estrany<br>
<br>
<a href="http://spir.wikidot.com/" target="_blank">http://spir.wikidot.com/</a><br>
<div><div></div><div>_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br><br clear="all"><br></div></div><div><div></div><div class="h5">-- <br>Regards,<br>Sithembewena Lloyd Dube<br><a href="http://www.lloyddube.com" target="_blank">http://www.lloyddube.com</a><br>

</div></div></blockquote></div><br><br clear="all"><br>-- <br>Regards,<br>Sithembewena Lloyd Dube<br><a href="http://www.lloyddube.com">http://www.lloyddube.com</a><br>