repr on a string

Alexander Schmolck a.schmolck at gmx.net
Mon Jun 2 06:11:17 EDT 2003


David Shochat <shochatd at yahoo.com> writes:

> On Sun, 01 Jun 2003 22:58:54 +0100, Alexander Schmolck wrote:
> 
> > David Shochat <shochatd at yahoo.com> writes:
> > 
> >> The Library Reference says this about built-in repr():
> >>  Return a string containing a printable representation of an object.
> >> 
> >> While studying Programming Python, 2nd Ed. Example 2-18, p. 84, I was
> > 
> > Haven't got the book, so I can't comment on the specific example, but see
> > below.
> > 
> Thanks for your reply (also to Gerrit Holl). I can see I need 
> to learn more about eval().
> 
> Anyway, here is a fragment of the book example I was 
> referring to, with a bit more context:
> 
> for dir in sys.path:
>     if string.find(os.path.abspath(dir), 'PyTools') != -1:
>         print 'removing', repr(dir)
>         sys.path.remove(dir)
> 
> In the 3d line, why does he not just say:
> 
>   print 'removing', dir
> 
> Why the call to repr() here?

My guess would be: mainly to put the name into (the right; ' or ") quotation
marks and make sure that funny characters are escaped. I think using `str`
here would be equally OK, although then the user wouldn't be able to tell
apart e.g. "that filename " from "that filename".

'as




More information about the Python-list mailing list