Movatterモバイル変換


[0]ホーム

URL:


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

Editing Code in the Standard REPL

00:00Editing Code in the Standard REPL.Most versions of the Python interpreter support code editing when running ininteractive mode.These editing features include code history and basic code completion.

00:14If you usually work on Unix-like systems, such as Linux and macOS,then you may be familiar with some of these features, which are present in theBash and Korn shells.

00:25These editing features are implemented using the GNU Readline library,which supports several useful styles of editing.First,a quick way to check whether or not you have code editing features available inyour current setup is to pressControl + P in the REPL’s prompt.

00:43This shortcut should show the last code instruction in your history.If nothing happens or^P appears on the screen as seen here,then code editing features aren’t available. Otherwise, you are ready togo.

01:05The standard REPL logs a complete history of all the code that you’ve typedand run while working in interactive mode.This history is saved to a file called.python_history,typically located in your home directory.

01:25While in interactive mode,you can browse this history using the arrow keys on your keyboard.With the up key, you can go back in history.With down, you can go forward in history.

01:41Once you find the desired line of code,you can pressEnter to confirm your selection and reuse the target instruction.Remember that every line in your code history keeps the original indentationthat you used when you first typed the code, which is convenient and saves time.

02:00The standard REPL provides basic completion capabilities for variable, object,and module names.This feature is automatically enabled when you start an interactive session.

02:13To invoke the available code completions,you can type the starting characters of a given name and press theTab key.This action triggers the completion search. If the search finds a match,then the name will automatically be completed for you, as seen here with theprint() command. You can then complete the line as desired.

02:37If the search finds more than one match,then you’ll have to pressTab again to get the entire list of matching names.The first time you pressTab, you may hear a beep, depending on your system.

02:48Once you’ve seen the options available, you can complete the line as desired,once again making use ofTab for completiononce you’ve typed a unique set of characters whichTab can complete.

03:04If nothing appears after pressingTab twice,then your search didn’t find any result, as seen here with the letterk.

03:15The code completion system looks at these objects when running its search:Python keywords; built-in function, class, and object names;currently defined names, such as variables, functions, and classes;and imported module and package names.

03:34When it comes to accessing attributes with dot notation, as seen on-screen,the code completion system will suggest completions from the target object’sattributes and methods.

03:46Here you can see codewhich has defined anObject class with.x and.y attributes,and a.show_values() method.

04:12With an instance of theObject class created,it’s possible to see available methods and attributes for the instance bypressingTab twice.

04:20The target attribute or method can then be typed and, if desired,completed withTab.

04:29The standard REPL provides a rather limited set of code editing features.However,they can be useful when you need to use an interactive session and don’t have amore advanced REPL.

04:40These code editing features can improve your productivity and make your codingexperience more pleasant.Learning keyboard shortcuts can significantly boost your productivity andefficiency when you’re working in a REPL session. For example,pressingControl + C on the REPL’s primary or secondary prompt cancels the inputand returns to the primary prompt.

05:12When you pressControl + C,the interpreter raises aKeyboardInterrupt exception and immediately returns tothe primary prompt.

05:24If you pressControl + C while code is running,then theKeyboardInterrupt exception interrupts the code’s execution and oncemore returns to the primary prompt.

05:33This behavior is useful when you launch a long-running task that you are notwilling to complete or when you accidentally run into an infinite loop.This example creates an infinitewhile loop.

05:48You can break the loop by pressingControl + C on your keyboard.After this key combination,you are once more back to the primary prompt, and the REPL session is ready for newinput.

06:02The standard REPL provides many other interesting and useful keyboard shortcuts,and the table on-screen shows some of them.These shortcuts will help you be more proficient when typing and editing code inan interactive session.

06:19In the next section of the course,you’ll look at how to get help while inside the REPL.

Avatar image for Anurag Gupta

Anurag Gupta onJuly 30, 2025

If code editing feature is not available then how to enable it?

Avatar image for Tappan Moore

Tappan MooreRP Team onJuly 31, 2025

@Anurag Gupta - are you on windows (like me)? I don’t think the ^p feature exists on windows, it’s only on unix-based systems (macos/linux).

Avatar image for Anurag Gupta

Anurag Gupta onAug. 4, 2025

Yes, I use Windows and thanks for the advice.

Become a Member to join the conversation.

Course Contents

Overview
62%

[8]ページ先頭

©2009-2026 Movatter.jp