Callv and Python Unpacking
Lesson 24 of 48 in Coddy's Python for GDScript Developers course.
GDScript Callables can receive an argument Array through callv. Python expands an iterable at a call with *values, collects extra positional arguments in *args, and separately collects keywords in **kwargs. Inside the Python function, args is a tuple and kwargs is a dictionary. A Python ** mapping must have string keys. The source example accepts a list explicitly; the target example demonstrates flexible argument packing.
GDScript
extends SceneTree
func total(values, factor = 1):
var result = 0
for value in values:
result += value
return result * factor
func _init():
print(total([2, 3], 2))
quit()Python
def total(*values, **options):
return sum(values) * options.get("factor", 1)
values = [2, 3]
print(total(*values, **{"factor": 2}))Porting trap
Do not pass one list when the API expects several positional arguments. Choose between f(values) and f(*values) based on the declared contract, not on the container's contents.
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 GDScript Developers
1From GDScript to Python
Run Your First Python ProgramPrint Is Not printsVar, Const, and BindingsInteger Division DifferencesSimilar Indentation, New ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and RebindingItems and enumerateWhile and Early ExitArray Callbacks and KeysRecap: Build a Ranking2Values and Decisions
Null and NoneThe is Keyword Changes MeaningLogical Results and OperandsConditional ExpressionsRecap: Eligibility Rules5Functions and Closures
Func, Def, and Returned ValuesKeyword Calls and DefaultsCallv and Python UnpackingCaptured Values and CallablesRecap: Refactor a Pricing Tool3Collections and Shared State
Array and List APIsArrays, Vectors, and TuplesDictionary Lookup RulesDuplicate and Shallow CopyRecap: Clean an Inventory6Input, Errors, and Modules
Console Work Outside GodotValidation and ExceptionsScript Resources and ImportsTyped GDScript and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler