Software Engineer (SDE)pythonbeginner3 interactive MCQs in app
Functions, Arguments, Lambda & Scope
mutable default argument trap*args / **kwargs unpackinglocal vs global scope
Core Concept
Functions encapsulate reusable logic with explicit parameters, pure return values, and predictable scope (LEGB rule: Local -> Enclosing -> Global -> Built-in). Always pass mutable defaults carefully: default argument values are evaluated ONCE at `def` time, NEVER on each call! Use `items=None` and initialize `items = [] if items is None else items` inside the body.
Worked Example
`def add_tag(tag, tags=None): tags = [] if tags is None else list(tags); tags.append(tag); return tags` prevents cross-call state leaks.
Common Pitfall
`def append_item(x, bucket=[])` shares the same list across every caller without arguments.
Recommended Readings & Practice
Practice Functions, Arguments, Lambda & Scope with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.