Cyril Verluise
09/13/2023, 9:43 PMreturn {item["refname"]: lambda: get_image(item["bbox"], parameters) for item in items}
However, when I do that, the refname and the bbox are messing up -> images (bbox) are saved under the wrong refnames (the refname of another image-bbox).
Quick fix
If I don't implement lazy loading, everything works as expected (expected image-bbox under the related refname)
return {item["refname"]: get_image(item["bbox"], parameters) for item in items}
That said, I still need lazy loading.
Set up
kedro version: 0.18.3
OS: mac
Questions
Can you confirm that my implementation should e correct? If yes, do we have any experience with this bug? Is there any known fix? Should I raise an issue?Nok Lam Chan
09/14/2023, 1:20 PMYolan Honoré-Rougé
09/14/2023, 8:42 PMlambda
not being properly redefined. I'll try to reproduce tomorrow and get back to you.Zhee
09/16/2023, 5:40 AMreturn {item["refname"]: lambda bbox=item["bbox"]: get_image(bbox, parameters) for item in items}
The template i use usually is the following (with an explicit parameter list.
return {
partition_key: (
lambda partition_load_func=partition_load_func, partition_key=partition_key: _my_function(
partition_load_func(),
partition_key,
parameters,
)
)
for partition_key, partition_load_func in loaded.items()
}
Yolan Honoré-Rougé
09/19/2023, 8:45 PMdef _create_lambda(bbox, parameters):
return lambda: get_image(bbox, parameters)
and then :
return {item["refname"]: _create_lambda(bbox, parameters) for item in items}
I haven't found the blog post about python scope for variables resolution which was an interesting read, but I did not find very carefully, if someone finds the reference please tell me!Cyril Verluise
09/19/2023, 8:47 PMreturn {
partition_key: (
lambda partition_load_func=partition_load_func, partition_key=partition_key: _my_function(
partition_load_func(),
partition_key,
parameters,
)
)
for partition_key, partition_load_func in loaded.items()
}
Awesome!Yolan Honoré-Rougé
09/20/2023, 8:33 AMCyril Verluise
09/20/2023, 9:24 AMNok Lam Chan
09/20/2023, 9:31 AMIn [7]: iterable = [lambda: print(x) for x in range(4)]
...:
...: for i in iterable:
...: i()
...:
...: print("Assign the variable to lambda scope")
...:
...: iterable = [lambda x=x : print(x) for x in range(4)]
...:
...: for i in iterable:
...: i()
...:
...:
3
3
3
3
Assign the variable to lambda scope
0
1
2
3
This StackOverFlow thread explains better: <https://stackoverflow.com/questions/938429/scope-of-lambda-functions-and-their-parameters>
Yolan Honoré-Rougé
09/20/2023, 11:16 AMNok Lam Chan
09/20/2023, 11:38 AMNote
section to warn about this. I just want to confirm this is not a bug that Kedro introduced. Actually should there be any lint tool that can pick this up? My guess is this should exists already.