What is the output of the following? “`python def counter(start=0): count = start def increment(): nonlocal count count += 1 return count return increment c = counter(10) print(c()) print(c()) “`

Python Developer Hard

Python Developer — Hard

What is the output of the following? “`python def counter(start=0): count = start def increment(): nonlocal count count += 1 return count return increment c = counter(10) print(c()) print(c()) “`

Key points

  • The `nonlocal` keyword allows modifying a variable in an outer scope
  • Each time `c()` is called, `count` is incremented by 1
  • The initial value of `count` is set to 10

Ready to go further?

Related questions