Keyword Calls and Defaults
Lesson 23 of 48 in Coddy's Python for GDScript Developers course.
Python supports keyword calls that bind values by parameter name, such as cost(quantity=3, price=4). The GDScript example uses ordinary positional calls and trailing defaults. Python evaluates a default expression when def executes; a default list is reused across later calls. To create a fresh list for each omitted argument, default to None and allocate inside the function.
GDScript
extends SceneTree
func cost(price, quantity = 1):
return price * quantity
func _init():
print(cost(4))
print(cost(4, 3))
quit()Python
def collect(items=[]):
items.append(1)
return len(items)
print(collect(), collect())Porting trap
Do not infer Python default lifetime from how a similar-looking GDScript declaration reads. Also, a Python argument supplied both positionally and by name raises TypeError rather than overriding an earlier value.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
Challenge
BeginnerImplement line_cost(price, quantity=1, discount=0). Return price * quantity - discount as an integer. Inputs are nonnegative integers and the discount never exceeds the product. Do not print or read input. The locked driver reads price, quantity, and discount on three lines. It calls your function with just price, with two positional arguments, and with all arguments named in a different order. It prints the three results on separate lines.
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 line_cost
price = int(input())
quantity = int(input())
discount = int(input())
print(line_cost(price))
print(line_cost(price, quantity))
print(line_cost(discount=discount, price=price, quantity=quantity))
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