How to find the methods that you need

So, in the code below, how did the programmer know all the methods he/she had to use to produce the plot output?

Well, part of that is experience, of course, and you will gain that experience with time, but the real secret here is called "code introspection" which is the process of:

(1) Finding out what methods to use, and

(2) Finding out how to use those methods

Let's look at (1) first.

When you have a program in mind, you should first think what type of objects you need. For example, if it's a program needed for teachers to maintain their student records, then you would think of string objects to represent the names of the students and list objects to keep all those strings together in one list. Maybe you also need integers or floats to store the student grades and so on. After that, you think about what actions the program should perform. For example, the program should print out the list of student names sorted in alphabetic order. So, now you should introspect to find out if there exists a method that sorts out lists alphabetically.

👉The way to find out what methods an object has is through the dir function:

Run the code above and you will see a full list of all available methods that can be applied to list objects. This is accomplished by passing the list type to dir.

Please ignore the methods starting and ending with double underscores. Those are methods used internally by the Python interpreter and are not of particular interest for us, programmers. If you look at the other "normal" methods, you will notice there is a sort method.

Alright, we found out (1) what method to use, but (2) how do we use that?

Well, you learned this already in one of the previous lectures. You can use the help function to find the documentation of a code element. Here is how we can use help to find the documentation of a method:

So, by passing list.sort to help we can get the help string of the sort method. In the help string you can see that the sort method can two optional arguments. In our case we don't need to change the values of these two optional-default arguments, so we can use sort() without any arguments inside as you can see in the code below:

If you run the above program, you will get a list printed out. If you compare the output list with the original list defined in line 1, you will notice the order of the list items has changed. The sort method manipulated the list in line 2 by changing the list order. Therefore, when we print students in line 3, we are going to get the updated list printed out. 

Similarly, in our plotting program, the title, xlabel, and ylabel methods are manipulating the pyplot object by adding a title to it, adding an x-axis label, and y-axis label, respectively. The plot method constructs the x and y axes, and the show method produces displays the plot in the output pane. So, all they are specialised to work on the pyplot object.

Feel free to use the empty editor below to experiment with the other list methods and also to find out what methods you can apply to strings and experiment with those two. Don't forget that methods are exclusive to the objects they belong to. That means string methods will not work with list methods and vice-versa.

Next Lecture
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.