Named Arguments and Defaults
Lesson 23 of 48 in Coddy's Python for Rust Developers course.
Rust ordinarily uses positional function arguments without default values. Python permits default parameter values and keyword calls that name arguments. A keyword call can reorder names, but each parameter must receive exactly one value. Positional arguments normally come before keyword arguments. Defaults are evaluated when the function is defined, not once per call. A mutable default list would therefore be shared across calls; use a None default and create the list inside instead.
Rust excerpt inside main
fn cost(price: i32, quantity: i32) -> i32 { price * quantity }
println!("{}", cost(4, 1));
println!("{}", cost(4, 3));Python
def cost(price, quantity=1):
return price * quantity
print(cost(4))
print(cost(quantity=3, price=4))Porting trap
The safe-default pattern is not needed for the immutable integer default shown. Avoid treating every default as a fresh Rust constructor expression.
Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.
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 Rust Developers
1From Rust to Python
Run Your First Python ProgramMacros to printBindings, mut, and ShadowingIntegers and Floor DivisionIndentation and ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Borrowed Iteration to forEntries and enumeratewhile, break, and continueIterators and ComprehensionsRecap: Build a Ranking2Values and Decisions
Option and NoneEquality and Object IdentityTruth Tests and FallbacksIf Expressions and elifRecap: Eligibility Rules5Functions and Closures
Functions and Shared ObjectsNamed Arguments and DefaultsPacking Calls and OptionsClosures and Capture TimingRecap: Refactor a Pricing Tool3Ownership and Collections
Vec to listTuples and DestructuringHashMap to dictClone and Shallow CopiesRecap: Clean an Inventory6Errors and Modules
Text Input and FormattingResult, Exceptions, and withModules and the Main GuardHints Without Borrow CheckingRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler