diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-11-21 01:31:02 (GMT) |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-11-21 01:31:02 (GMT) |
commit | 17fc44c9b3040538839f63d344b98ad5a904d520 (patch) | |
tree | ae2aa634c7133227e622996c010be273cce7fbe5 /Doc/reference | |
parent | 39bdad813a2f07bd8c373e2c88b76467ef12832c (diff) | |
download | cpython-17fc44c9b3040538839f63d344b98ad5a904d520.zip cpython-17fc44c9b3040538839f63d344b98ad5a904d520.tar.gz cpython-17fc44c9b3040538839f63d344b98ad5a904d520.tar.bz2 |
Improve str() and object.__str__() documentation (issue #13538).
Diffstat (limited to 'Doc/reference')
-rw-r--r-- | Doc/reference/datamodel.rst | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 322e8c8..8bbbc99 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1124,10 +1124,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 @@ -1141,18 +1142,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? |