On 15 November 2012 17:13, Chris Kaynor <span dir="ltr"><<a href="mailto:ckaynor@zindagigames.com" target="_blank">ckaynor@zindagigames.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div class="im">On Thu, Nov 15, 2012 at 8:04 AM, Kevin Gullikson<br>
<<a href="mailto:kevin.gullikson@gmail.com">kevin.gullikson@gmail.com</a>> wrote:<br>
> Hi all,<br>
><br>
> I am trying to make a dictionary of functions, where each entry in the<br>
> dictionary is the same function with a few of the parameters set to specific<br>
> parameters. My actual use is pretty complicated, but I managed to boil down<br>
> the issue I am having to the following example:<br>
><br>
> In [1]: def test_fcn(a, x):<br>
>    ...:     return a*x<br>
>    ...:<br>
><br>
> In [2]: fcn_dict = {}<br>
><br>
> In [3]: for i in [1,2,3]:<br>
>    ...:     fcn_dict[i] = lambda x: test_fcn(i, x)<br>
>    ...:<br>
<br>
</div>In this case, I would recommend using functools.partial instead of a lambda.<br>
It will solve this problem (although MRAB's solution will as well), is<br>
trivially faster (likely insignificant in any real application), and,<br>
IMO, clearer:<br>
<br>
for i in [1,2,3]:<br>
    fcn_dict[i] = functools.partial(test_fcn, i)<br>
<br>
Note that this only works if you are either only specifying the first<br>
arguments by position, or specifying arguments by keyword. There is no<br>
way to specify the second argument by position; you'd have to pass it<br>
as a keyword argument.<br></blockquote><div><br></div><div>Another way to do this is by making a factory function:</div><div><br></div><div>>>> def factor_multiplier(factor):</div><div>...     def factor_multiply(target):</div>

<div>...             return factor * target</div><div>...     return factor_multiply</div><div>... </div><div><br></div><div>Testing it:</div><div><br></div><div>>>> trippler = factor_multiplier(3)</div><div>>>> trippler</div>

<div><function factor_multiplier.<locals>.factor_multiply at 0x7ffeb5d6db90></div><div>>>> trippler(10)</div><div>30</div><div>>>> doubler = factor_multiplier(2)</div><div>>>> doubler(15)</div>

<div>30</div><div>>>> doubler(trippler(1))</div><div>6</div><div>>>> </div><div><br></div><div>Solving your problem:</div><div><br></div><div>>>> function_dict = {}</div><div>>>> for i in range(100):</div>

<div>...     function_dict[i] = factor_multiplier(i)</div><div>... </div><div>>>> </div><div>>>> function_dict[42](2)</div><div>84</div><div>>>> function_dict[20](3)</div><div>60</div><div>>>> </div>

<div><br></div><div>This is definitely longer that Chris' approach, but it's more powerful overall. It's worth learning and using both.</div><div><br></div><div>In a sense, you were close, but you were just not catching the variable:</div>

<div><br></div><div><div>>>> function_dict.clear()</div><div>>>> for i in range(100):</div><div>...     function_dict[i] = (lambda i: lambda x: x*i)(i)</div><div>... </div><div>>>> function_dict[19](2)</div>

<div>38</div><div>>>> </div></div></div></div>