Menu
CoddyTech

Explicit Variadic Arguments

Lesson 24 of 48 in Coddy's Python for Dart Developers course.

Python star parameters gather positional extras into a tuple, and double-star parameters gather keyword extras into a dict. Dart commonly accepts a collection parameter instead of an arbitrary variadic argument list. At Python call sites, star expansion turns iterable elements into positional arguments and double-star expansion turns string-key mapping entries into keyword arguments. Dart spread syntax inside a collection literal builds that collection; it does not give every function Python-style variadic calls. Keep the API boundary clear when translating a collection-based Dart utility to a Python function that receives separate values.

Dart

int total(List<int> values, {int factor = 1}) {
  return values.fold(0, (sum, value) => sum + value) * factor;
}
void main() {
  print(total([2, 3], factor: 2));
}

Python

def total(*values, **options):
    print(type(values).__name__)
    return sum(values) * options.get('factor', 1)
print(total(*[2, 3], **{'factor': 2}))

Porting trap

Passing a list and expanding a list are different calls. Match the function signature rather than treating spread syntax as universal.

challenge icon

Challenge

Beginner

Implement scaled_total(*values, **options). All values are integers. Return their sum multiplied by options.get("factor", 1). Ignore other option names. Do not mutate collections or print. The driver reads a line of integer tokens (possibly empty) and a factor on the next line. It prints a call with only unpacked values, then a call with unpacked values and a dictionary containing the factor.

Your function definitions go in functions.py. The locked driver.py imports them and handles the supplied input/output and checks. You do not need to edit the driver.

Try it yourself

from functions import scaled_total

values = [int(part) for part in input().split()]
factor = int(input())
print(scaled_total(*values))
print(scaled_total(*values, **{"factor": factor, "note": "ignored"}))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Dart Developers

Practice on your own: Online Python compiler