Modules, Classes, and Methods

In Importing Modules, we saw that a module is a kind of object, one that can contain functions and other variables. There is another kind of object that is similar to a module: a class. You’ve been using classes all along, probably without realizing it: a class is how Python represents a type.

You may have called built-in function help on int, float, bool, or str. We’ll do that now with str (notice that the first line says that it’s a class):

 >>>​​ ​​help(str)
 Help on class str in module builtins:
 
 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.
 
  [Lots of other names with leading and trailing underscores not shown here.]
 
  | capitalize(...)
  | S.capitalize() -> str
  |
  | Return a capitalized version of S, i.e. make the first character
  | have upper case and the rest lower case.
  |
  | casefold(...)
  | S.casefold() -> str
  |
  | Return a version of S suitable for caseless comparisons.
  |
  | center(...)
  | S.center(width[, fillchar]) -> str
  |
  | Return S centered in a string of length width. Padding is
  | done using the specified fill character (default is a space)
  |
  | count(...)
  | S.count(sub[, start[, end]]) -> int
  |
  | Return the number of non-overlapping occurrences of substring sub in
  | string S[start:end]. Optional arguments start and end are
  | interpreted as in slice notation.
 
 [There are many more of these as well.]

Near the top of this documentation is this:

 | str(object[, encoding[, errors]]) -> str
 |
 | Create a new string object from the given object.

That describes how to use str as a function: we can call it to create a string. For example, str(17) creates the string ’17’.

We can also use str to call a method in class str, much like we call a function in module math. The main difference is that every method in class str requires a string as its first argument:

 >>>​​ ​​str.capitalize(​​'browning'​​)
 'Browning'

This is how methods are different from functions: the first argument to every string method must be a string, and the parameter is not described in the documentation for the method. This is because all string methods require a string as the first argument, and more generally, all methods in a class require an object of that class as the first argument. Here are two more examples, this time using the other two string methods from the ​code​​​. Both of these also require a string as the first argument.

 >>>​​ ​​str.center(​​'Sonnet 43'​​,​​ ​​26)
 ' Sonnet 43 '
 >>>​​ ​​str.count(​​'How do I love thee? Let me count the ways.'​​,​​ ​​'the'​​)
 2

The first method call produces a new string that centers ’Sonnet 43’ in a string of length 26, padding to the left and right with spaces.

The second method call counts how many times ’the’ occurs in ’How do I love thee? Let me count the ways.’ (once in the word thee and once as the penultimate word in the string).