Sobes.tech
Junior — Middle

What methods can be used to make a function flexibly configurable with parameters?

sobes.tech AI

Answer from AI

To make a function flexibly configurable using parameters in Python, you can use:

  • Positional and named parameters with default values.
  • *args arguments for passing an arbitrary number of positional parameters.
  • **kwargs arguments for passing an arbitrary number of named parameters.

Example:

def greet(name, greeting="Hello", *args, **kwargs):
    punctuation = kwargs.get('punctuation', '!')
    print(f"{greeting}, {name}{punctuation}")

# Usage:
greet("Ivan")  # Hello, Ivan!
greet("Ivan", greeting="Hi", punctuation=".")  # Hi, Ivan.

This approach allows calling the function with different sets of parameters, making it highly flexible.