Named Calls and Defaults
Lesson 23 of 48 in Coddy's Python for Dart Developers course.
Dart signatures distinguish optional positional and named parameters; named call arguments use a colon. Python ordinary parameters can usually be supplied positionally or by keyword with an equals sign, unless the signature explicitly restricts them. Dart default values must be compile-time constants. Python default expressions run when def executes and can produce mutable objects that persist across calls. Use None and allocate a fresh list inside when independent per-call state is intended. Supplying a Python parameter both positionally and by keyword raises TypeError rather than allowing the later value to overwrite it.
Dart
int cost(int price, {int quantity = 1}) => price * quantity;
void main() {
print(cost(4));
print(cost(4, quantity: 3));
}Python
def collect(items=[]):
items.append(1)
return len(items)
print(collect())
print(collect())Porting trap
A Python mutable default is shared state, not a fresh optional collection. Dart's constant-default restriction prevents that exact declaration pattern.
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 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