<br><br><div class="gmail_quote">On Thu, May 27, 2010 at 2:52 AM, kirby urner <span dir="ltr">&lt;<a href="mailto:kirby.urner@gmail.com">kirby.urner@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">&gt;<br>
&gt; Much appreciated André!<br></div></blockquote><div><br>It was a fun challenge.<br> :-) <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">
&gt;<br>
&gt; I think our solutions are similar:<br>
&gt;<br>
<br>
</div>... yours is better though, in that you don&#39;t make use of exec.<br>
<br>
My use of it was superfluous.<br>
<br>
Changing my solution, in light of yours:<br>
<br>
#===<br>
<div class="im"><br>
def makeroot(N):<br>
   try:<br>
       assert type(N)==type(1) and N&gt;=0<br>
   except:<br>
       raise ValueError(&quot;0 &lt;= N &lt;= integer&quot;)<br>
<br>
   fname = &quot;root&quot; + str(N)<br>
<br>
   if N==0:<br>
</div>       globals()[fname] = lambda x: pow(x, 0)<br>
   else:<br>
       globals()[fname] = lambda x: pow(x, float(1)/N)<br>
<br>
<br>
for i in range(11):<br>
    makeroot(i)<br>
<br>
#===<br>
<font color="#888888"><br></font></blockquote><div><br>Technically, your use of globals() instead of locals(), like I did, is better, in that it allows you to have it inside a function body, and still be available at the module level, as per your stated goal.<br>
<br>However, I find the use of lambda to be too restrictive, in principle, as it obviously works only for expressions, and not complex functions.  If this example is to be used as a prototype to show students how to do this kind of name assignment, then the way I have done it with an inner named function (which I first learned about *years* ago, from a post from you on edu-sig!  I believe it was <a href="http://mail.python.org/pipermail/edu-sig/2005-March/004590.html">http://mail.python.org/pipermail/edu-sig/2005-March/004590.html</a>) is a better way to go.<br>
 </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><font color="#888888">
Kirby<br>
</font></blockquote></div><br>