Sorry to resurrect an old thread, but I wanted to say that I agree it would be great to have better docstrings for the built-in types.
I started poking at this a little bit this morning, and I put together a quick little test implementation that adds docstrings to str’s dunder methods using the strategy @skirpichev suggested above (thanks!) and a script in Tools to attach the new docstrings to the built-in types at startup.
I don’t want to worry about the particular phrasing of the new docstrings yet, but rather just to get a sense of whether this is something worth devoting time to (and if there are better ideas for how to organize the code).
For reference:
First part of help(str) before
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __format__(self, format_spec, /)
| Return a formatted version of the string as described by format_spec.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __getnewargs__(...)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mod__(self, value, /)
| Return self%value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
First part of help(str) after
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to 'utf-8'.
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
| Implements self + value.
|
| Return the concatenation of self and value.
|
| __contains__(self, value, /)
| Implements value in self.
|
| Return True if the given value is a substring of self, and False
| otherwise.
|
| Equivalent to self.find(value) >= 0.
|
| __eq__(self, value, /)
| Return self==value.
|
| __format__(self, format_spec, /)
| Return a formatted version of the string as described by format_spec.
|
| __ge__(self, value, /)
| Implements self >= value.
|
| Comparisons of strings use lexicographical ordering. self and value are
| compared character by character using their Unicode code points. If all
| compared characters are equal, the longer string is considered to be
| greater.
|
| __getitem__(self, key, /)
| Implements self[key].
|
| If key is an integer, return a length-1 string containing the
| character at that index in self.
|
| If key is a slice, return the corresponding substring of self.
|
| __getnewargs__(self, /)
|
| __gt__(self, value, /)
| Implements self > value.
|
| Comparisons of strings use lexicographical ordering. self and value are
| compared character by character using their Unicode code points. If all
| compared characters are equal, the longer string is considered to be
| greater.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implements iter(self).
|
| Return an iterator over the characters of self. The iterator yields
| successive length-1 substrings, each corresponding to a single Unicode
| character in self.
|
| __le__(self, value, /)
| Implements self <= value.
|
| Comparisons of strings use lexicographical ordering. self and value are
| compared character by character using their Unicode code points. If all
| compared characters are equal, the longer string is considered to be
| greater.
|
| __len__(self, /)
| Implements len(self).
|
| Return the number of Unicode characters in self.
|
| __lt__(self, value, /)
| Implements self < value.
|
| Comparisons of strings use lexicographical ordering. self and value are
| compared character by character using their Unicode code points. If all
| compared characters are equal, the longer string is considered to be
| greater.
|
| __mod__(self, value, /)
| Implements self % value.
|
| Return a new string printf-style string formatting, replacing '%'
| conversion specifications in self with zero or more elements in the
| given value. Alternative options for string formatting include
| str.format, or template strings.
|
| __mul__(self, value, /)
| Implements self * value.
|
| Return a new string containing the contents of self repeated a number of
| times given by value, which must be an integer.
|
| __ne__(self, value, /)
| Return self!=value.