Vararg and Keyword Packing
Lesson 24 of 48 in Coddy's Python for Kotlin Developers course.
Kotlin vararg collects positional arguments and the spread operator expands an array. Python *args collects positional arguments into a tuple; **kwargs collects named arguments into a dictionary. At a call site, a single star expands an iterable and double star expands a string-keyed mapping into keyword arguments. This is not the same as treating a Kotlin Map as arbitrary named parameters.
Kotlin
fun total(vararg values: Int, factor: Int = 1) = values.sum() * factor
fun main() {
val values = intArrayOf(2, 3)
println(total(*values, factor = 2))
}Python
def total(*values, **options):
return sum(values) * options.get("factor", 1)
values = [2, 3]
print(total(*values, **{"factor": 2}))Porting trap
A Python ** mapping must have string keys and must not duplicate an already bound argument. Keep the specified parameter names when the driver calls by keyword.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
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 Kotlin Developers
1From Kotlin to Python
Run Your First Python ProgramPrint and String TemplatesVal, Var, and Python NamesDivision and Integer WidthSuites and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and Loop BindingsEntries and withIndexUpdates and Loop ControlCollection PipelinesRecap: Build a Ranking2Values and Decisions
Nullable Types and NoneEquality and IdentityBoolean Conditions and orIf Expressions and ElifRecap: Eligibility Rules5Functions and Closures
Functions and Shared ListsDefaults and Named CallsVararg and Keyword PackingClosures and Lambda ResultsRecap: Refactor a Pricing Tool3Collections and Shared State
Read-only Lists and MutationPair and Tuple UnpackingMap Lookup and Missing KeysShallow Copies and SetsRecap: Clean an Inventory6Input, Errors, and Modules
Console Input and FormattingExceptions and Resource ScopeImports and Entry PointsStatic Types and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler