[Tutor] Why difference between printing string & typing its object reference at the prompt?

boB Stepp robertvstepp at gmail.com
Thu Oct 4 05:11:19 CEST 2012


Thanks to all who responded. There was much more going on here than I
ever would have suspected. I am glad I asked the questions I did. This
has been very informative.

On Tue, Oct 2, 2012 at 11:53 PM, Dave Angel <d at davea.name> wrote:

>
> There are two operations supported by (most) objects that produce a
> string.  One is exemplified by the str() function, which converts an
> object to a string.  That's the one called implicitly by print().  This
> form just represents the data, in the form most likely to be needed by
> the end user.

What happens if str() or repr() is not supported by a particular
object? Is an exception thrown, an empty string returned or something
else I am not imagining?

>
> The other operation is repr(), which attempts to produce a string that
> could be used in a program to reproduce the actual object.  So a repr()
> will have quote marks artificially added, or brackets, or commas, or
> whatever seems appropriate for the particular object.  This is intended
> for the programmer's use, not for the end user.

What larger phrase does "repr" stand for? My text mentions
"representational form" later in the book, which sounds similar in
concept to what you are discussing.

[...]
> Your question was about string objects, but I tried to make the
> explanation as generic as possible.  Those two functions, str() and
> repr(), are used, or implied in many places.  For example, if you print
> a list, it'll call str() on the whole list.  But the list object's logic
> will in turn call repr() on each of its elements, and put the whole
> thing together with braces and commas.
>

As I go along in my study of Python will it become clear to me when
and how repr() and str() are being "...used, or implied in many
places"?


Thanks!
boB


More information about the Tutor mailing list