A
A
albertalexandrov2018-10-04 08:14:49
Python
albertalexandrov, 2018-10-04 08:14:49

[closures] Why doesn't the outer function argument need to be specified as nonlocal in the inner function?

Hey!
There is a closure:

def outer(x):
  count = 555
  num = 666
  def inner():
    nonlocal count, num
    return x
  return inner


muller = outer(777)
print(muller())
print(muller.__closure__[0].cell_contents, muller.__closure__[1].cell_contents, muller.__closure__[2].cell_contents)

As far as I understand, the variable x gets into __closure__ only when it is used in inner?
Why shouldn't it be specified as nonlocal along with count, num?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
Us Feel, 2020-04-11
@FeelUs

global and nonlocal must be specified if you are going to change the variable.
If you're just reading it, there's no need for it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question