- In this video I'd like to take a moment
to talk about methods which are actually
functions that belong to objects.
So, methods and functions are really the same concept,
the difference is that functions are defined to stand
alone, whereas methods belong to a class.
And, in a later lesson, we'll talk about defining
your own classes, but there are tons of existing classes
that already have been defined for you as part of
the Python standard library.
In fact, we used the decimal class previously to do
monetary calculations,
and in this example,
I'd like to use a string object, which is an instance
of Python's string class,
to demonstrate this concept of methods.
So let's say I have a string called "Hello".
This is actually a string object containing the characters
that you see here and s is simply a variable that refers
to, or points to, if you will, that string object
someplace in memory. Now, when you have an object,
you can quote, unquote, talk to it, by invoking its methods.
So, if I have s, which refers to the object containing
the string Hello, I can use a dot and then I can tell it
to do something. So, the string class that implements
strings in Python, for example, has a function called
lower, which gives you back a lowercase representation
of that string. Now, strings are immutable,
so it doesn't modify the original string, it simply
gives me back a new string containing all of those
letters in lowercase. Similarly, I can do s.upper(),
which will give me back an uppercase version of that string.
Now, for every class that's part of the Python
standard library, when you go to python.org
and look at the Python standard library reference,
you're going to be able to look at each individual class
in a given module and then within
that class's documentation, it's going to show you
the methods that are available to you.
So, for instance,
if I go over to python.org
and we'll pull up the documentation webpage, here.
Right, so here's the Library Reference.
Now, string is a little bit different from other classes
that are out there, in that the type string is built into
the Python language as the type str, so, for the built-in
types you can see this section within the documentation
for the Python standard library, and here's the type
string, str, which we use for processing strings.
And if you scroll down through this page,
you're going to see there's a section on string methods
and then it shows you all the different methods that
you can call. In these first two cases they happen to be
methods that take no arguments, but it gives
descriptions of the methods.
Let's scroll down. Here is an example of a method
that takes an argument, actually, several different
methods that take arguments and when you see
this notation with square brackets, what this means
is that the center function expects an argument
that represents a field width, in this case,
for centering a string in a field width.
But the second argument is optional. So the square
brackets indicate optional arguments in the context
of the online documentation.
In this case, we have a required first argument
and then we have optional second and third arguments
where you can specify the first argument, you can
optionally specify the second argument and not
a third argument, in this case, or you can specify
three arguments. Notice the nested use of square
brackets in that particular piece of
online documentation.
So, if you really want to get a sense of everything
that you can do for a string in Python, you need to
take a look at this page that I'm showing you
right now, so that you can see all the different
functions that are available to you
and you can get a sense of everything that you can do.
I would definitely recommend at least reading
the names of all the functions, generally they're
named in a way where you can kinda figure out
what they do without having to fully read
the documentation, but for any class you're going
to use, excuse me, you are going to wanna take a
close look at its online documentation so you
really know everything the class can do.