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
BeginnerImplement 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"}))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules5Functions and Closures
Arrows and Explicit ReturnsNamed Calls and DefaultsExplicit Variadic ArgumentsClosures and nonlocalRecap: Refactor a Pricing Tool3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler