Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Getting Help and Introspecting Code in the Standard REPL

00:00 Getting Help and Introspecting Code in the REPL. An important feature of any coding environment such as an IDE, editor, or REPL is the possibility of getting quick help and guidance about using the language, libraries, and tools that you are working with.

00:15 If you are working in the standard REPL, then you’ll have a few tools that allow you to get help and introspect your code depending on your needs and specific context.

00:24 The built-in help() function gives you access to Python’s built-in help system. You can use this function by calling it in two ways: with an object or string as an argument, which gives you access to the object’s help page, or with no arguments, which enters Python’s help system.

00:41 The help page of an object typically contains information from the object’s docstrings. It may also include a list of methods and attributes. For example, here’s a fragment from the page of the str class that you can access as seen on-screen. In this example, you use the str class object as an argument to help().

01:03 You can use the up and down keys to move through the page. When you get to the desired information, you can press Q to exit the help viewer. If you use a string as an argument to help(), then the help system looks for it as the name of a module, function, class, method, keyword, or documentation topic.

01:25 This way to call help() comes in handy when the target object isn’t available in your current namespace. For example, let’s say you want to get help on the pathlib module, but you haven’t imported it yet.

01:38 If you try the command scene on-screen, it won’t work. pathlib hasn’t been imported, so Python says that pathlib is not defined.

01:47 However, you can run help() with the string "pathlib" as an argument. Then you’ll get output similar to what’s seen on-screen.

02:04 Once more, Q will allow you to exit the help system. The second way to use help() is to call the function without arguments. This way, you enter the built-in help system.

02:21 Once you run it, you’ll immediately note that the prompt changes from three chevrons (>>>) To help>. This new prompt reminds you that you are in the interactive help mode.

02:31 In help mode, you can enter keywords, module names, function names, or any other name. The help system will search for the target name and present the associated documentation page. To try this out, type sys on the help> prompt and then press Enter.

02:47 You’ll get a page similar to what’s seen on-screen. Once more, to exit the page, press Q on your keyboard. The help system allows you to search for different topics.

03:01 Just type the desired topic and press Enter. If the topic has a documentation page, then you’ll get it on-screen. Here you first search for the term keywords.

03:15 This search finds and shows the page on Python keywords. Next, searching for the term iterable.

03:26 Unfortunately, this topic doesn’t have a documentation page, so you get an ImportError that takes you out of the help system.

03:38 Once you’ve found the required information, then you can exit the help system by typing Q or quit and pressing Enter.

03:44 This way, you’ll be back to your REPL session. When you’re working in a REPL session, you have direct access to some Python built-in tools that you can use to introspect your code and obtain more information and context on the objects that you are working with. Some of these tools are seen on-screen.

04:03 You can use any of these built-in functions to introspect your code and retrieve useful information that you can later use in your coding process. For example, let’s say you are working with dictionaries and want to get a list of all the methods and attributes in the class.

04:20 The code seen on-screen will do this for you.

04:26 Here, the output shows a list of names as strings. The list will include any attributes and methods defined in the dict class. You can use this built-in function with any Python object.

04:41 The vars() function works similarly to dir(), but returns a dictionary of name-object pairs instead of a list.

05:08 The locals() and globals() functions can also be useful when you want to know the names defined in a given scope in your code.

05:27 Finally, the type() function helps you determine the data type or class of a given object within your code.

05:53 In the next section of the course, you’ll go deeper into your knowledge of the REPL by looking at customization.

Become a Member to join the conversation.