Built-In Classes
Python's built-in classes support a variety of representations of data, together with operations that are suitable for those representations. The built-in classes includes numerical types as well as those implementing containers or collections, that is, objects that can hold groups of other objects. Before diving into some of this, let's first examine more how to get information about different classes (whether built-in or custom defined).
To retrieve information on a class, we use the help() function. For example, here is what is returned by help(str)
:
After hitting the spacebar a few times to scroll down the list, we start seeing the names of useful-looking methods such as upper
and lower
. Just as the help documentation indicates, the way to invoke methodname on a string object strg_obj is to enter strg_obj.methodname(arguments), like this:
This kind of "dot" syntax for invoking a method on an object is familiar from OOP languages such as C++ and Java. In Python, the dot operator ( . ) is used for accessing attributes in the namespace of an object (both data attributes and methods, which are functions attached to objects).
You can also get a list of the contents (i.e., all the names in the namespace) of class str
:
For help on a specific content element, say the upper
method of the str
class:
Note that, as per the help, S.lower()
and S.upper()
return a string resulting from the specified operation being performed on S
. But S
, the string itself, has not changed. Strings are immutable sequence objects and do not have methods that change the object itself. This is not necessarily the case for mutable objects such as lists.