E ExamMaster

Engineering Python · Functions & Files

Scope and Defaults

In Engineering Python because lab helpers take optional settings — local scope and default arguments cover the everyday cases without decorators.

Names assigned inside a function are local. Default parameter values fill in when the caller omits an argument. Decorators are out of scope here.

  • Engineering Python
  • Medium level
  • 4 concepts

1Local scope

Assignments inside a function create local names. They do not rewrite globals of the same spelling unless you use special declarations (avoid those in this course).

Figure. Assignments inside a function create local names. bump assigns x = 2 and returns it. Outer x = 1 is still 1 — the local x is separate.

Local vs outer

x = 1

def bump():
    x = 2
    return x

print(bump(), x)
After a function assigns `x = 2` locally, what is outer `x = 1`?
  1. Still 1 — the local x is separate
  2. Becomes 2 automatically
  3. Is deleted

Local assignment does not rewrite the outer name.

2Default arguments

`def f(n, scale=1):` lets callers omit `scale`. Defaults are evaluated at def time — use immutable defaults (numbers, strings, None), not `[]`.

Figure. def scaled(n, factor=10) lets callers omit factor. scaled(3) uses 10 and returns 30. scaled(3, 2) overrides factor and returns 6. Defaults are evaluated at def time — use immutable defaults, not [].

Takeaway

  1. Idea`def f(n, scale=1):` lets callers omit `scale`. Defaults are evaluated at def time — use immutable defaults (numbers, strings, None), not `[]`.

Default

def scaled(n, factor=10):
    return n * factor

print(scaled(3))
print(scaled(3, 2))

3Why defaults help labs

Defaults keep call sites short for the common case while still allowing overrides for experiments.

In teaching notebooks the default is the 'usual' setting; passing an argument is the deliberate experiment.

Figure. Defaults keep call sites short for the common case while still allowing overrides for experiments. The default is the usual setting; passing an argument is the deliberate experiment.

What is the main teaching reason for a default argument?
  1. Short common call sites, with an easy override for experiments
  2. To ban keyword arguments
  3. To make return None

Defaults encode the usual case without hiding the override.

4Lab: call with a default

Use a defaulted helper and print both call styles.

Figure. greet(name, punct='!') returns name+punct. greet('lab') omits punct, so the default '!' fills in and the printed result is lab!.

Takeaway

  1. IdeaUse a defaulted helper and print both call styles.

Coding lab. Default argument runs in the app, with checks on your output.

Notes

  • In Engineering Python because lab helpers take optional settings — local scope and default arguments cover the everyday cases without decorators.
  • Assignments inside a function create local names. They do not rewrite globals of the same spelling unless you use special declarations (avoid those in this course).
  • `def f(n, scale=1):` lets callers omit `scale`. Defaults are evaluated at def time — use immutable defaults (numbers, strings, None), not `[]`.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

Locals stay inside the function; defaults fill omitted arguments; avoid mutable defaults.

Local scope
Assignments inside a function create local names. They do not rewrite globals of the same spelling unless you use special declarations (avoid those in this course).
Default arguments
`def f(n, scale=1):` lets callers omit `scale`. Defaults are evaluated at def time — use immutable defaults (numbers, strings, None), not `[]`.
Why defaults help labs
Defaults keep call sites short for the common case while still allowing overrides for experiments.
Lab: call with a default
Use a defaulted helper and print both call styles.

Practise Scope and Defaults

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 2 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.