3.4. MethodsΒΆ
Classes have special functions defined only for their own members called class methods, or simply methods. Methods are called on an object by following that object with .methodname()
. We saw methods like .isround()
in action above. Built-in classes like str
have methods too!
# The upper() method changes all characters in a string to uppercase
introduction = 'Hi, my name is...'
introduction.upper()
'HI, MY NAME IS...'
# The isdigit() method checks if all characters in a string are digits
'12345'.isdigit()
True
Using the help()
function on a class shows all methods available to instances of that class. The __methods__
are private methods used internally by Python. Skip down to capitalize(...)
to see the methods available to us.
help(str)
# The capitalize method capitalizes the fist letter of the str
'united states'.capitalize()
There are many methods of objects in Python which can be handy, but to give us a better sense of how methods work, we will first have to talk about Functions.