Embercode
✕
Functions and ScopeQuick quiz

Shadowing and globals in Python

8 questions. Covers Static locals and counters in Python.

Questions 1 to 5 of 8

  1. 1. What does this program print? (one line per output) ``python class Box: def __init__(self): self.size = 5 def grow(self, amount): size = amount size += 2 def grow2(self, amount): size = 0 size += amount self.size += size def __str__(self): return f"size={self.size}" b = Box() b.grow(10) print(b) b.grow2(3) print(b) ``
  2. 2. What does this program print? (one line per output) ``python count = 0 def bump(): count = 0 for i in range(3): count += 1 print("local", count) bump() bump() print("global", count) ``
  3. 3. What does this program print? ``python next_id = 100 def new_id(): global next_id current = next_id next_id = next_id + 1 return current a = new_id() b = new_id() print(a, b, next_id) ``
  4. 4. A function has a local variable named limit. Which expression reads the module-level variable limit from inside that function?
  5. 5. What does shout() print? ``python def shout(): name = "outer" f = lambda name: name.upper() print(f("inner"), name) shout() ``

5 question(s) left on this page.