Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A Python function is a first-class object that encapsulates a block of parameterized statements. It is defined using the def keyword, executes within its own local lexical scope upon invocation, and yields a value to the caller via the return statement, implicitly evaluating to None if no return is specified.

Base Syntax

Functions are declared with the def keyword, followed by an identifier, a parenthesized parameter list, and a colon. The function body must be indented.
def function_name(param1: int, param2: str = "default") -> bool:
    """Optional docstring for metadata."""
    # Lexical block / Function body
    return True

Parameter and Argument Mechanics

Python supports highly flexible parameter binding mechanisms. Arguments can be passed positionally or by keyword.
  • Default Parameters: Assigned at function definition time, not at execution time. (Note: Using mutable objects like list or dict as defaults results in the same object being shared across all calls).
  • Arbitrary Positional Arguments (*args): Captures excess positional arguments into a tuple.
  • Arbitrary Keyword Arguments (**kwargs): Captures excess keyword arguments into a dict.

Parameter Modifiers (Python 3.8+)

You can enforce how arguments are passed using special syntax markers / and *.
def strict_function(pos_only, /, standard, *, kw_only):
    pass


# pos_only: Must be passed by position.

# standard: Can be passed by position or keyword.

# kw_only: Must be passed by keyword.

Return Mechanics

The return statement terminates function execution and passes an object back to the caller.
def return_multiple():
    return 1, 2, 3  # Implicitly packs values into a tuple: (1, 2, 3)

def implicit_return():
    x = 5
    # Implicitly returns None

Scope and Namespaces (LEGB Rule)

When a variable is referenced inside a function, Python resolves the name using the LEGB rule, searching in this exact order:
  1. Local: Names assigned within the function.
  2. Enclosing: Names in the local scope of any enclosing functions (closures).
  3. Global: Names assigned at the top-level of the module.
  4. Built-in: Names preassigned in the built-in names module.
To modify variables outside the local scope, Python provides specific keywords:
  • global: Binds a local identifier to the module-level namespace.
  • nonlocal: Binds a local identifier to the nearest enclosing lexical scope (used in nested functions/closures).
x = 10
def outer():
    y = 20
    def inner():
        global x
        nonlocal y
        x += 1  # Modifies global x
        y += 1  # Modifies enclosing y
    inner()

First-Class Object Characteristics

Functions in Python are instances of types.FunctionType. They possess attributes (like __name__ and __doc__) and can be treated as data. This allows for higher-order functions:
  • Assigned to variables.
  • Passed as arguments to other functions.
  • Returned from other functions.
def execute_callback(func, value):
    return func(value) # Executing a passed function

Lambda Functions

Python supports anonymous, inline functions via the lambda keyword. Lambdas are restricted to a single expression and cannot contain statements or annotations. The result of the expression is implicitly returned.

# Syntax: lambda parameters: expression
multiply = lambda x, y: x * y
Master Python with Deep Grasping Methodology!Learn More