Getting help on functions and methods

The ‘help’ function, when applied to a function, gives help on it!

help(sorted)

Help on built-in function sorted in module builtins:

sorted(iterable, key=None, reverse=False)

Return a new list containing all items from the iterable in ascending order.

A custom key function can be supplied to customise the sort order, and the

reverse flag can be set to request the result in descending order.

The ‘dir’ function, when applied to an object, lists all its attributes (properties and methods):

dir("Hexxo")

[‘__add__’,

‘__class__’,

‘__contains__’,

‘__delattr__’,

‘__dir__’,

‘__doc__’,

‘__eq__’,

‘__format__’,

‘__ge__’,

‘__getattribute__’,

‘__getitem__’,

‘__getnewargs__’,

‘__gt__’,

‘__hash__’,

‘__init__’,

‘__iter__’,

‘__le__’,

‘__len__’,

‘__lt__’,

‘__mod__’,

‘__mul__’,

‘__ne__’,

‘__new__’,

‘__reduce__’,

‘__reduce_ex__’,

‘__repr__’,

‘__rmod__’,

‘__rmul__’,

‘__setattr__’,

‘__sizeof__’,

‘__str__’,

‘__subclasshook__’,

‘capitalize’,

‘casefold’,

‘center’,

‘count’,

‘encode’,

‘endswith’,

‘expandtabs’,

‘find’,

‘format’,

‘format_map’,

‘index’,

‘isalnum’,

‘isalpha’,

‘isdecimal’,

‘isdigit’,

‘isidentifier’,

‘islower’,

‘isnumeric’,

‘isprintable’,

‘isspace’,

‘istitle’,

‘isupper’,

‘join’,

‘ljust’,

‘lower’,

‘lstrip’,

‘maketrans’,

‘partition’,

‘replace’,

‘rfind’,

‘rindex’,

‘rjust’,

‘rpartition’,

‘rsplit’,

‘rstrip’,

‘split’,

‘splitlines’,

‘startswith’,

‘strip’,

‘swapcase’,

‘title’,

‘translate’,

‘upper’,

‘zfill’]

Most of these are confusing methods beginning and ending with __, part of the internals of python.

Again, just as with error messages, we have to learn to read past the bits that are confusing, to the bit we want:

"Hexxo".replace("x", "l")

‘Hello’

help("FIsh".replace)

Help on built-in function replace:

replace(…) method of builtins.str instance

S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring

old replaced by new. If the optional argument count is

given, only the first count occurrences are replaced.

Next: Reading: Operators