[Tutor] a print puzzle

Steven D'Aprano steve at pearwood.info
Thu Dec 2 22:36:56 CET 2010


bob gailer wrote:
> On 12/2/2010 2:09 PM, Richard D. Moores wrote:
> 
> [snip]
>>
>> I found nothing that helped as much as you have already.
>>
> In the reference (for 2.6.2) under print statement I find:
> 
> "print evaluates each expression in turn and writes the resulting object 
> to standard output (see below). If an object is not a string, it is 
> first converted to a string using the rules for string conversions.
> 
> This leaves the reader pondering "rules for string conversions". I 
> wonder whether this is defined and why it was not cross-referenced here.

I'm not sure why there's no cross-reference, or even where it should 
cross-reference to, but the rules for string conversion are that:

if the object has a method __str__, it is called and the result used;
if not, but it has a method __repr__, it is called and the result used;
if not (only relevant for old-style classes in Python 2.x), then Python 
has a default string format.


-- 
Steven



More information about the Tutor mailing list