Python Glossary Русский

all Python 3Functionsterm

def

definition

A function declaration with the def keyword. The body of the function is set off by indentation.

Syntax

def имя(параметры):
    тело

def is executed when control reaches it, and default argument values are evaluated once — at definition time, not on every call. So a mutable default (def f(x, acc=[])) is shared across calls and accumulates data; the fix is acc=None plus building the list inside the body.

Examples

def greet():
    print('Hello!')
greet()  # → Hello!
def add(a, b):
    return a + b
print(add(3, 4))  # → 7
def say(name):
    """Docstring: первая строка тела — описание функции."""
    print(f'Hi, {name}')
say('Alice')  # → Hi, Alice
def nothing():
    pass  # тело-заглушка
print(nothing())  # → None — функция без return

See also

Open in the full glossarydocs.python.orgReport an inaccuracy