Skip to content

Commit 255cef6

Browse files
committed
Dynamic lambda loop bug
Making lambda functions in loops causes unintended side effects
1 parent 78e4304 commit 255cef6

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

ngcsimlib/compilers/process_compiler/component_compiler.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
from ngcsimlib.compartment import Compartment
33
from ngcsimlib.compilers.utils import compose
44

5+
def __make_get_arg(a):
6+
return lambda current_state, **kwargs: kwargs.get(a, None)
7+
8+
def __make_get_param(p, component):
9+
lambda current_state, **kwargs: component.__dict__.get(p, None)
10+
11+
def __make_get_comp(c, component):
12+
return lambda current_state, **kwargs: current_state.get(component.__dict__[c].path, None)
13+
14+
515
def compile(transition_method):
616
composition = None
717
component = transition_method.__self__
@@ -27,14 +37,15 @@ def compile(transition_method):
2737
composition = compose(composition, op_compile(conn))
2838

2939
arg_methods = []
40+
print(compartments)
3041
for a in args:
31-
arg_methods.append((a, lambda current_state, **kwargs: kwargs.get(a, None)))
42+
arg_methods.append((a, __make_get_arg(a)))
3243

3344
for p in parameters:
34-
arg_methods.append((p, lambda current_state, **kwargs: component.__dict__.get(p, None)))
45+
arg_methods.append((p, __make_get_param(p, component)))
3546

3647
for c in compartments:
37-
arg_methods.append((c, lambda current_state, **kwargs: current_state.get(component.__dict__[c].path, None)))
48+
arg_methods.append((c, __make_get_comp(c, component)))
3849

3950
def compiled(current_state, **kwargs):
4051
kargvals = {key: m(current_state, **kwargs) for key, m in arg_methods}

0 commit comments

Comments
 (0)