• [PyCharm IDE]
Skip to end of metadata
Go to start of metadata

As you already know, PyCharm helps completing your source code. Besides the basic and smart type code completion, PyCharm automatically generates indented code, inserts the paired braces and quotes. Let's explore this behavior in details.


Type the class declaration, and then press Enter:

PyCharm has stopped at an indented location, where you can start typing the function declaration:

Indentation is generated automatically. In our case, function declaration (and all the nested operations at each level) is indented 4 spaces to the right. Why? The Python code is generated according to a certain code style, accepted in your company, and PyCharm helps maintain this code style. Let's see how it's done, using this small example. Open the Settings dialog, expand the node Code Style, and then open the page Python: Ctrl+Alt+SProject Settings→Code Style →Python:

As you see here, the indent size is exactly four spaces. PyCharm uses this indentation for automatic generation of the code, and for reformatting. It is quite possible to change the indent size (as well as the other code style settings) - for example, if you specify indent size 5 spaces, all your newly generated code will indent 5 spaces to the right from the previos indentation level. Explore the Smart Keys page for more details.

However, code style schemes PyCharm comes with, are not editable. When you apply the modified settings, PyCharm automatically creates a copy of an existing scheme:

This new scheme resides at your user home; you can share this scheme among your team mates, and if they use it, they will thus follow the same style of coding. Learn more about location of the code style settings on the page Project and IDE Settings.

Auto-completing syntax

When you type the opening parenthesis (, you see that PyCharm automatically adds the closing one ), and generates the mandatory parameter self. After pressing Enter, the caret moves to the next typing position (indented according to the code style), where you want to print a message. Type an opening quote - PyCharm automatically adds the closing quote, while the caret rests between the quotes, allowing you to type a message string:

Let's now explore a more complicated case - edit a Django template. With a rather complicated syntax of the Django templates, automatic generation of the closing tags is really vital. So, start typing a Django tag:

As soon as you type the opening characters, PyCharm automatically generates the closing characters:

So doing, the caret rests between the two % characters, where you have to type the code:

Same relates to the opening {{ characters - PyCharm automatically generates the closing ones.

This behavior is configurable in the Smart Keys page of the Editor settings: Ctrl+Alt+SIDE Settings→Editor→Smart Keys:

For example, to have PyCharm generate closing parenthesis or square bracket, make sure that the check box Insert pair bracket is selected. For PyCharm generating the closing quote, it is necessary to select the check box Insert pair quote. For the Django templates, there is the dedicated check box Auto-insert closing }} and %} in Django templates.

Other topics of this tutorial

Your Rating: Results: PatheticBadOKGoodOutstanding! 19 rates

  • No labels