summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-11-21 01:45:51 (GMT)
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-11-21 01:45:51 (GMT)
commit5fae0e58549c9270b188a1591ffa6cc4c2d9ab4e (patch)
tree5e955cf6e54ee57cf3531687ddf4c01681c34786 /Doc/reference
parent9ddfb19e4197be4abc0b74236f207a3efc853c04 (diff)
downloadcpython-5fae0e58549c9270b188a1591ffa6cc4c2d9ab4e.zip
cpython-5fae0e58549c9270b188a1591ffa6cc4c2d9ab4e.tar.gz
cpython-5fae0e58549c9270b188a1591ffa6cc4c2d9ab4e.tar.bz2
Improve str() and object.__str__() documentation (issue #13538).
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/datamodel.rst28
1 files changed, 18 insertions, 10 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index d093383..b7a6736 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1140,10 +1140,11 @@ 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
+.. method:: object.__repr__(self)
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
@@ -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?