Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Python Function Parameter Types

Tech 1

Formal vs. Actual Parameters

Formal parameters are defined in the function signature and used within the function body, while actual parameters are the values passed during a function call.

def display_stars(num_stars):
    for star in range(num_stars):
        print('*')

display_stars(5)

In this example, num_stars is the formal parameter, and 5 is the actual parameter passed during the call.

Parameter Categories

1. Required Parameters (Positional Arguments)

Thece parameters must be provided when calling the function and are defined without default values.

def repeat_message(message, times):
    for _ in range(times):
        print(message)

repeat_message('Hello', 3)

Omitting any required parameter results in an error:

repeat_message('Hello')  # TypeError: missing required argument

2. Default Parameters

Parameters with predefined values that become optional during function calls.

def show_value(value=50):
    print(value)

show_value(75)  # Output: 75
show_value()    # Output: 50

When multiple parameters exist, default parameters must follow required parameters in the definition.

3. Keyword Arguments

Arguments passed using key=value syntax, allowing parameter order flexibility.

def calculate_values(x, y, z=0, w=1):
    print(x, y, z, w)

calculate_values(y=30, x=15, z=5)

4. Variable-Length Parameters

Functions can accept arbitrary numbers of arguments using special syntax:

  • *args: Captures extra positional arguments as a tuple
  • **kwargs: Captures extra keyword arguments as a dictionary
def collect_parameters(*args, **kwargs):
    print('Positional:', args)
    print('Keyword:', kwargs)

collect_parameters(10, 20, name='Alice', score=95)

Output:

Positional: (10, 20)
Keyword: {'name': 'Alice', 'score': 95}

Combined Parameter Usage

All parameter types can coexist in a single function definition with specific ordering rules.

def example_function(req_param, def_param=100, *var_args, **var_kwargs):
    print('Required:', req_param)
    print('Default:', def_param)
    print('Variable positional:', var_args)
    print('Variable keyword:', var_kwargs)

# Function call demonstration
example_function(5, 150, 300, 400, key1='value1', key2='value2')

The output demonstrates how different parameter types interact:

Required: 5
Default: 150
Variable positional: (300, 400)
Variable keyword: {'key1': 'value1', 'key2': 'value2'}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.