Gurobipy can apparently read the index of a list comprehension formulated within the parentheses of a function. How does this work? Shouldn't this formulation pass a generator object to the function? How do you read the index from that?
md = gp.Model()
md.addConstrs(True for i in [1,2,5,3])
The output contains the indices that where used in the list comprehension formulation:
{1: <gurobi.Constr *Awaiting Model Update*>,
2: <gurobi.Constr *Awaiting Model Update*>,
5: <gurobi.Constr *Awaiting Model Update*>,
3: <gurobi.Constr *Awaiting Model Update*>}
I am not sure if I understand your question correctly, but if you are wondering how you can retrieve the iterator from generator expression, then that's by accessing
<generator>.gi_frame.f_locals.The
gi_framecontains theframeobject corresponds to the generator expression and it hasf_localsattribute which denotes the local namespace seen by this frame.You can even use the more direct API
inspect.getgeneratorlocals.But please do note that: