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"><<a href="mailto:zebra05@gmail.com">zebra05@gmail.com</a>></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"><<a href="mailto:denis.spir@free.fr" target="_blank">denis.spir@free.fr</a>></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's excellent explanation:<br>
<br>
On Mon, 22 Feb 2010 10:01:06 +1100<br>
Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>> wrote:<br>
<br>
[...]<br>
<div><br>
> So if you write a pathname like this:<br>
><br>
> >>> path = 'C:\datafile.txt'<br>
> >>> print path<br>
> C:\datafile.txt<br>
> >>> len(path)<br>
> 15<br>
><br>
> It *seems* to work, because \d is left as backlash-d. But then you do<br>
> this, and wonder why you can't open the file:<br>
<br>
</div>I consider this misleading, since it can only confuse newcomers. Maybe "lonely" single backslashes (not forming a "code" 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>
> But if the escape is not a special character:<br>
><br>
> >>> s = 'abc\dz' # nothing special<br>
> >>> print s<br>
> abc\dz<br>
> >>> print repr(s)<br>
> 'abc\\dz'<br>
> >>> len(s)<br>
> 6<br>
><br>
> The double backslash is part of the *display* of the string, like the<br>
> quotation marks, and not part of the string itself. The string itself<br>
> only has a single backslash and no quote marks.<br>
<br>
</div>This "display" is commonly called "representation", 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>
>>> s = "\\"<br>
>>> s<br>
'\\'<br>
>>> print s, len(s)<br>
\ 1<br>
>>> print repr(s), len(repr(s))<br>
'\\' 4<br>
>>><br>
The string holds 1 char; its representation (also a string, indeed) holds 4.<br>
<div><br>
> The best advice is to remember that Windows allows both forward and<br>
> backwards slashes as the path separator, and just write all your paths<br>
> using the forward slash:<br>
><br>
> 'C:/directory/'<br>
> 'C:textfile.txt'<br>
<br>
</div>Another solution is to take the habit to always escape '\' 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>