Merge from 3.3: Improve str() and object.__str__() docs (issue #13538).

This commit is contained in:
Chris Jerdonek 2012-11-20 17:53:17 -08:00
commit d675a2c48a
7 changed files with 100 additions and 49 deletions

View file

@ -1140,11 +1140,12 @@ Basic customization
modules are still available at the time when the :meth:`__del__` method is
called.
.. index::
single: repr() (built-in function); __repr__() (object method)
.. method:: object.__repr__(self)
.. index:: builtin: repr
Called by the :func:`repr` built-in function to compute the "official" string
representation of an object. If at all possible, this should look like a
valid Python expression that could be used to recreate an object with the
@ -1157,18 +1158,25 @@ Basic customization
This is typically used for debugging, so it is important that the representation
is information-rich and unambiguous.
.. index::
single: string; __str__() (object method)
single: format() (built-in function); __str__() (object method)
single: print() (built-in function); __str__() (object method)
.. method:: object.__str__(self)
.. index::
builtin: str
builtin: print
Called by :func:`str(object) <str>` and the built-in functions
:func:`format` and :func:`print` to compute the "informal" or nicely
printable string representation of an object. The return value must be a
:ref:`string <textseq>` object.
Called by the :func:`str` built-in function and by the :func:`print` function
to compute the "informal" string representation of an object. This differs
from :meth:`__repr__` in that it does not have to be a valid Python
expression: a more convenient or concise representation may be used instead.
The return value must be a string object.
This method differs from :meth:`object.__repr__` in that there is no
expectation that :meth:`__str__` return a valid Python expression: a more
convenient or concise representation can be used.
The default implementation defined by the built-in type :class:`object`
calls :meth:`object.__repr__`.
.. XXX what about subclasses of string?