Functions defined inside if blocks don't do variable lookup correctly
If I define an inner function inside an if block, it fails to look up values that it closes over, doing e.g. a global lookup instead:
value = 4
def make_func(value):
if value == 0:
def inner():
log.info(f"value = {value}")
return inner
make_func(0)()
---
value = 4
Interestingly, however, if there is an inner function defined outside the if block, the one inside the if block works as it should.
value = 4
def make_func(value):
if value == 0:
def inner():
log.info(f"value = {value}")
return inner
def fix():
pass
make_func(0)()
---
value = 0
This is a side effect of the way function definitions work in pyscript. Under def inner you can try adding nonlocal value. I've had some luck with this.
Nope:
value = 4
def make_func(value):
if value == 0:
def inner():
nonlocal value
log.info(f"value = {value}")
return inner
make_func(0)()
---
value = 4
I have tested this myself (and every variation I can think of). It is indeed broken. Hopefully @craigbarratt can fix it.
Yikes. It doesn't correctly detect inner functions inside nested statements, which then means the correct variable scoping isn't applied. I pushed a fix f4f32f4.
A workaround for the existing code is to define a dummy inner function at the top-level, eg:
value = 4
def make_func(value):
if 1:
def real_inner():
log.info(f"value = {value}")
return real_inner
def dummy_inner():
pass
@LeszekSwirski Has your problem been fixed? Could you please update the ticket or close it? Thank you
Honestly, I still just have the workaround in my own config, but from the looks of it it's fixed.