PyCharm 2024.3 Help

Python code insight

Code insight is a common name used for auto-completion, intention actions, type inference, and other techniques related to the code analysis in PyCharm.

Syntax highlighting

The PyCharm editor respects highlighting of the keywords, comments, parameters, type hints, and other elements.

Code highlighting
class Car: """This class represents a car object.""" def __init__(self, speed=0): self.speed = speed self.odometer = 0 self.time = 0 # self.passengers = ['Lena', 'Benjamin', 'Tom'] def say_state(self): """Prints the current speed in kilometers per hour.""" print("I'm going {} kph!".format(self.speed))

The particular highlighting colors are defined in the Editor | Color Scheme page of the Settings dialog.

With new Python versions, PyCharm supports more specific types and language structures,for example, Python 3.10 specific pattern matching:

Pattern matching
from dataclasses import dataclass @dataclass class Point: x: int y: int def where_is(point): match point: case Point(x=0, y=0): print("Origin") case Point(x=0, y=y): print(f"Y={y}") case Point(x=x, y=0): print(f"X={x}") case Point(): print("Somewhere else") case _: print("Not a point")

Code completion

PyCharm supports Code completion.

Code completion

As PyCharm indexes your whole project on each startup, it allows you to autocomplete any existing entity wherever it is defined.

Intentions

PyCharm can find and highlight various problems, locate dead code, find probable bugs, spelling problems, and hint some Python specific improvements, and align the code structure.

As soon as the IDE finds a way to improve your code, it displays a yellow bulb icon yellow bulb icon in the editor next to the current line. You can also press Alt+Enter to invoke the context actions menu.

For example, you can invert an if statement with a condition.

Inverting an if statement
def foo(): return True if foo(): print("This is true") else: print("This is false")

For more information, refer to Intention actions.

PyCharm allows you to navigate to a location where a particular named code reference has been first declared. Place the caret at the symbol in the editor and press Ctrl+B. Alternatively, use Ctrl+Click.

View declaration

For more information, refer to the Go to declaration and its type section for details, and the entire Source code navigation section to learn about the other types of navigation.

Show expression type

While analyzing your code, you often need to identify the type of a particular expression. Consider the following code:

def f(x: str): print(chr(x.index('o')) * 42 + 'xxx')

Press Ctrl+Shift+P and select the target expression from the list.

Select an expression

The expression type will be shown in the tooltip.

Preview expression type

To preview the definition of the selected expression, press Ctrl and hover over the expression.

Preview type definition

You can also use Find Action Ctrl+Shift+A and start typing Expression.

Checking regular expressions

If your code contains a regular expression, there is an intention action to check it: place the caret at the regular expression and press Alt+Enter. The RegExp checker shows up, where you can type the string and check if it matches the regular expression:

Checking regular expressions
import re def is_valid_email(email: str) -> bool: email_regex = re.compile("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$)") return bool(re.match(email_regex, email))

For more information, refer to this section.

Viewing reference documentation

With PyCharm, you don’t need to surf the web every time you stumble across some alias, or search your whole application for a method declaration you can’t remember. Place the caret at a keyword you want to look up, and press Ctrl+Q:

Quick documentation
from math import sqrt def square_and_root(x): square = x ** 2 root = sqrt(x) return square, root

For more information, refer to Quick Documentation.

Last modified: 10 October 2024