-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtip4.py
More file actions
21 lines (18 loc) · 944 Bytes
/
tip4.py
File metadata and controls
21 lines (18 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# example fn. for processing transaction
def process_payment(transaction_id: int, amount: float, currency: str, description: str = None):
print(f"Processing transaction {transaction_id}...")
print(f"Amount: {amount} {currency}")
if description:
print(f"Description: {description}")
# Usage
process_payment(1234, 100.0, 'USD', 'Payment for services')
# enforce keyword-only arguments to minimize errors
# make the optional `description` arg keyword-only
def process_payment(transaction_id: int, amount: float, currency: str, *, description: str = None):
print(f"Processing transaction {transaction_id}:")
print(f"Amount: {amount} {currency}")
if description:
print(f"Description: {description}")
# Usage
process_payment(1234, 100.0, 'USD', description='Payment for services')
process_payment(5678, 150.0, 'EUR', 'Invoice payment') # throws error as we try to pass in more positional args than allowed!