Your reviewer wants summary stats for the full dataset and for each treatment group separately — same function, different scope. How do you handle that without writing two functions?
make_table_row from Day 17 wraps the formatting. For flexible scope, I'd need a parameter that defaults to "all groups" but can be overridden to a specific group name.
Default arguments. group: str = "all" means callers who don't pass a group get the full dataset. Callers who pass "control" get only that group. The function handles both cases in one body:
def summarize_group(respondents: list, group: str = "all") -> tuple:
if group == "all":
subset = respondents
else:
subset = [r for r in respondents if r["treatment_group"] == group]What's the -> tuple return type? And how does tuple unpacking work?
A tuple is a fixed-length sequence — (n, mean_outcome, mean_age). Return it and the caller can unpack it: n, mean, age = summarize_group(respondents). Three values out of one function call, each going to its own variable. Reviewers love this because the call site is readable:
def summarize_group(respondents: list, group: str = "all") -> tuple:
subset = respondents if group == "all" else [r for r in respondents if r["treatment_group"] == group]
n = len(subset)
mean_outcome = round(sum(r["outcome"] for r in subset) / n, 2) if n else 0.0
mean_age = round(sum(r["age"] for r in subset) / n, 2) if n else 0.0
print(f"Group '{group}': n={n}, mean_outcome={mean_outcome}, mean_age={mean_age}")
return (n, mean_outcome, mean_age)So n, mean, age = summarize_group(respondents, "control") is the whole SPSS group-level Descriptives, in one line.
One line. No filter dialog, no copy-paste, no screenshot.
And if I call it without the second argument, I get the full-sample stats. Default args are basically a research shortcut.
Default args have one trap: never use a mutable object as a default — def f(x, lst=[]) reuses the same list across all calls. Use None and create the list inside the function if you need a mutable default.
def f(x, y=10): # y defaults to 10
return x + y
f(5) # 15
f(5, 3) # 8def stats() -> tuple:
return (87, 4.72, 34.2)
n, mean, age = stats() # unpacks into three named variablesNever: def f(lst=[]) — the list is shared across all calls. Instead: def f(lst=None) → lst = lst or [].
Your reviewer wants summary stats for the full dataset and for each treatment group separately — same function, different scope. How do you handle that without writing two functions?
make_table_row from Day 17 wraps the formatting. For flexible scope, I'd need a parameter that defaults to "all groups" but can be overridden to a specific group name.
Default arguments. group: str = "all" means callers who don't pass a group get the full dataset. Callers who pass "control" get only that group. The function handles both cases in one body:
def summarize_group(respondents: list, group: str = "all") -> tuple:
if group == "all":
subset = respondents
else:
subset = [r for r in respondents if r["treatment_group"] == group]What's the -> tuple return type? And how does tuple unpacking work?
A tuple is a fixed-length sequence — (n, mean_outcome, mean_age). Return it and the caller can unpack it: n, mean, age = summarize_group(respondents). Three values out of one function call, each going to its own variable. Reviewers love this because the call site is readable:
def summarize_group(respondents: list, group: str = "all") -> tuple:
subset = respondents if group == "all" else [r for r in respondents if r["treatment_group"] == group]
n = len(subset)
mean_outcome = round(sum(r["outcome"] for r in subset) / n, 2) if n else 0.0
mean_age = round(sum(r["age"] for r in subset) / n, 2) if n else 0.0
print(f"Group '{group}': n={n}, mean_outcome={mean_outcome}, mean_age={mean_age}")
return (n, mean_outcome, mean_age)So n, mean, age = summarize_group(respondents, "control") is the whole SPSS group-level Descriptives, in one line.
One line. No filter dialog, no copy-paste, no screenshot.
And if I call it without the second argument, I get the full-sample stats. Default args are basically a research shortcut.
Default args have one trap: never use a mutable object as a default — def f(x, lst=[]) reuses the same list across all calls. Use None and create the list inside the function if you need a mutable default.
def f(x, y=10): # y defaults to 10
return x + y
f(5) # 15
f(5, 3) # 8def stats() -> tuple:
return (87, 4.72, 34.2)
n, mean, age = stats() # unpacks into three named variablesNever: def f(lst=[]) — the list is shared across all calls. Instead: def f(lst=None) → lst = lst or [].
Elias needs a flexible summary function that returns N, mean outcome, and mean age as a tuple. Write `summarize_group(respondents, group='all')` — when `group='all'`, summarise all respondents; when a group name is given, filter to that group first. Return `(n, mean_outcome, mean_age)`.
Tap each step for scaffolded hints.
No blank-editor panic.