4.12 Methods: Functions That Belong to Objects

A method is simply a function that you call on an object using the form

object_name.method_name(arguments)

For example, the following session creates the string variable s and assigns it the string object 'Hello'. Then the session calls the object’s lower and upper methods, which produce new strings containing all-lowercase and all-uppercase versions of the original string, leaving s unchanged:


In [1]: s = 'Hello'

In [2]: s.lower() # call lower method on string object s
Out[2]: 'hello'

In [3]: s.upper()
Out[3]: 'HELLO'

In [4]: s
Out[4]: 'Hello'

The Python Standard Library reference at

https://docs.python.org/3/library/index.html

describes the methods of built-in types and the types in the Python Standard Library. In the “Object-Oriented Programming” chapter, you’ll create custom types called classes and define custom methods that you can call on objects of those classes.