[Tutor] New class, how return value of x in interactive shell

Alan Gauld alan.gauld at btinternet.com
Mon Apr 5 22:00:40 CEST 2010


"Vincent Davis" <vincent at vincentdavis.net> wrote

> Take a look at the repr and str methods:
> http://docs.python.org/reference/datamodel.html#basic-customization
>
> Ok so I am still a little confused, It seems that __str__ is used for 
> print
> and str()

That's right and repr() is used when evaluating the object, as at the >>> 
prompt.

So

>>> print b # calls b.__str__()

whereas

>>> b # calls b.__repr__()

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list