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

Unlock This Lesson

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

Unlock This Lesson

View Function Signatures

00:00 Function Signatures, Docstrings, and Source Code Preview.

00:05 When you type an opening parenthesis to call a function or method in bpython, it will display the corresponding function signature with its formal parameters and their default values.

00:16 It will also show information on which ones are positional, positional-only, keyword, or keyword-only arguments.

00:34 As you provide values for your function, bpython highlights the current parameter name in the function signature to indicate how many are left. This can be really helpful when the function expects multiple arguments.

00:46 Unfortunately, this feature doesn’t support type hints so that types of arguments or the function’s return value aren’t shown at all, even if they exist in the source code.

00:56 Function signatures already provide plenty of useful information that can help you understand what a function or method does without having to look up its documentation. However, bpython goes the extra mile by displaying a docstring from the function’s body if it can find and extract one from the source code. In this example, both the sub() function and Python built-in max() function have docstrings that bpython shows.

01:23 A docstring is usually a multiline string literal that immediately follows the function signature and contains a human-readable description of the function.

01:31 Sometimes it may include details about the function arguments or automated doctests to self-test and demonstrate how to use the function. Automatically displaying docstrings is more efficient than using Python’s built-in help() function or accessing the function’s .__doc__ attribute.

01:48 If neither the function signature nor the docstring is enough for you, then you can use bpython to reveal the underlying source code. Many contemporary code editors let you navigate to the definition of a symbol by clicking on it or holding a designated key.

02:04 This works equally well for symbols that you defined in your project and ones that are defined in the Python standard Library or a third-party package installed with pip. In bpython, you can display a read-only preview of the corresponding source code by pressing F2. After typing a given symbol,

02:23 you can type the name of a module, function, or class. Depending on the type of symbol, you’ll only see the source code belonging to that particular scope.

02:34 The source code preview opens in your operating system’s default terminal pager program, which is usually the Unix less command. You can use the arrow keys or Page Up and Page Down to scroll up and down.

02:46 Searching is also possible with a forward slash and then N for the next occurrence or P for the previous one.

02:55 press Q to quit. Whether you use a code editor or bpython, this feature will only work as long as there’s pure Python source code available. If the symbol that you’re interested in is a built-in function, or is implemented as an extension module in the C programming language, then you won’t be able to get any information about it. Instead, bpython will display a message that no source code was found.

03:25 In the next section of the course, you’ll learn how bpython can help you when the inevitable happens and mistakes are made.

Become a Member to join the conversation.