Named Arguments and Defaults
Lesson 23 of 48 in Coddy's Python for C# Developers course.
Use named arguments and defaults without sharing accidental mutable state.
C# reference excerpt
Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.
static int Cost(int price, int quantity = 1) { return price * quantity; }
Console.WriteLine(Cost(quantity: 3, price: 4));Python
def cost(price, quantity=1):
return price * quantity
print(cost(quantity=3, price=4))What changes
C# uses name: value at a call; Python uses name=value. Both support omitted optional arguments, but Python default expressions are evaluated once when def executes. C# optional defaults have restricted constant/default-value forms; an arbitrary new List<int>() is not an optional parameter default. For a fresh Python list per call, use None as the default and create [] inside.
Transfer mistake
def collect(items=[]) shares one list between calls. Also, Python does not choose method overloads by signature: defining the same function name again rebinds that name.
Your task
Implement line_cost(price, quantity=1, discount=0), returning price * quantity - discount. Inputs are nonnegative integers and discount is no larger than the product. The driver reads three lines and checks calls with price only, two positional arguments, and reordered named arguments, printing each result. Preserve the exact parameter names and defaults.
Challenge
BeginnerImplement line_cost(price, quantity=1, discount=0), returning price * quantity - discount. Inputs are nonnegative integers and discount is no larger than the product. The driver reads three lines and checks calls with price only, two positional arguments, and reordered named arguments, printing each result. Preserve the exact parameter names and defaults.
Editable implementation files: functions.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.
Keep the supplied imports and exact API names. Return results or raise the required exceptions; do not read input or print from these implementation modules.
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 C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules5Functions and Scope
Define Functions and ReturnNamed Arguments and DefaultsStar Arguments and UnpackingCallbacks and Python ScopeRecap: Pricing Functions3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler