From the course: More Python Tips, Tricks, and Techniques for Data Science

Accessing methods and documentation - Python Tutorial

From the course: More Python Tips, Tricks, and Techniques for Data Science

Start my 1-month free trial

Accessing methods and documentation

- One of the key things that you should know as a data scientist is how to figure out the right methods and attributes to be used for a particular kind of scenario. Now in this video, we'll be looking at different methods and short codes for accessing documentation of functions, and object. So first off, we have the Help Function. Now, Python has this built in function that can access the doc strings of the functions and objects defined in Python or any of the imported packages. So for example, let's say, I want to learn about the sum function in Python. So all I need to do is simply type Help, and then pass the name of the function. So let's say SAM here, then run the cell. And it will display the doc string, the documentation that is defined in the SAM function in Python. Now, similarly, if I want to learn about the read CSV function from the pandas package, so all I need to do is first of all, import the read CSV function. So let's say I import from pandas, import read underscore CSV, and then I can pass on this function, read underscore CSV, to my Help function, run the cell, and it will display the doc strings that are defined inside the read CSV function in pandas package. Now, I have the shorthand for this Help function. That is a question mark character. So let's say if I want to learn about the SAM function without using the Help function, so all I need to do is simply add a question mark ahead of it, and run the cell. And a pop up from the bottom of the screen comes up. And it'll give you all the information about the signature of that function. Doc string, that is the documentation and its type. You can exit by clicking on the cross map. Now, we can use this shorthand of question mark to learn about objects as well. So let's say I create my list with the three elements, let's say one three five. And now I want to learn about this list. What is it? So all I need to do is simply type the name of the list and add this shorthand, which is the question mark, and run the cell and it will pop up. Again, the type is list, we have a list object, the string farm, which is the values that the list contains length, and there are doc strings. Now, the next part is TAB to AutoComplete. Now many at times you are not aware of what all you can do with an object or what classes and functions you can import from a package. For this Ipython has a very useful interface, which is the use of the TAB key for auto completion and exploration of the contents of objects, modules, and namespaces. So let's say we have this list object with us. So all I need to do is simply type my list Dot Operator, and then I hit TAB key on my keyboard to list down all the methods that are available for me to use with this list object. So I have been clear copy, so on and so forth. Now let's say I want to use the Append function, but I do not really know what that function does. So I can again, use the question mark character, the shorthand, so simply use Append function, but I don't know how to use it. So add the question mark character head of it, run the cell. And again, you can see the signature, the doc string, and the type comes up from the bottom. And it will tell you what that function does. Now similarly, we can use the TAB AutoComplete for importing different functions classes from packages. So let's say I want to import something, but I don't really know what so I can simply do from pandas input. And I will list down all the functions and classes that I can import from pandas. So hit TAB on the keyboard, and it will display lists down all the functions and classes for you to use and import. Now, if you want to narrow down the list, as in, you're not actually remembering the right method to import. So simply Type R and then hit TAB. So it will list down all the methods that are starting from R. Accordingly, you can pick up whichever one you want to use. So that's how TAB to AutoComplete works. Now if we try out these methods on our Custom function, so we have this Custom function here, define, which is list of squares. Now this function returns a new list containing squares of all the elements of the list that is passed to it. So let's say I invoke this method or if I want to learn about list of squares, and you should make sure that you run the cell, list of squares, add a question mark ahead of it, run the cell. And you see the doc string that we define is now available in the doc string read key in the pop up. So the pop up tells you that this is a function that returns a new list. All right? So if we want to access the source code. Now, besides the documentation, if you want to view the source code of a function, for debugging purposes, or to understand the underlying code, you can access the source code of a Python function with double question marks. So let's say we are again, using the same function list of squares, add two question marks instead of one, run the cell. And this pop up will now contain the source the code for that function that you would want to look at. So that's how you can access the source code for Python function. But the limitation with this is that you can only access the documentation or the source code of the function that is defined in Python. So if I want to actually learn about the sum function, and it's code, I won't be able to do that, because this function is actually defined in C. So that's why I do not see the source here. So that's the limitation. All right. So we saw how we can use a few functions and shorthands to access documentation, listing available methods and view source code of a function or class in Python. Now, go ahead and get some hands on practice. And I'll see you in the next video.

Contents