<span class="gmail_quote"><br></span>Thanks, Adam. I guess the exec would be exec(&quot;some_func&quot;).<br>
The result seems pretty similar to eval(), allthough eval() seems more
straight-forward if the aim is to assign the returned value (&quot;Done&quot;) to a variable.<br>
<br>
&gt;&gt;&gt; eval(&#39;some_func(&quot;wasn\&#39;t that cool&quot;)&#39;)<br>
Hello World wasn&#39;t that cool<br>
&#39;Done&#39;<br>
<br>
in other words<br>
<br>
&gt;&gt;&gt; s = eval(&#39;some_func(&quot;wasn\&#39;t that cool&quot;)&#39;)<br>
Hello World wasn&#39;t that cool<br>
&gt;&gt;&gt; s<br>
&#39;Done&#39;<br>
<br><br><div><span class="gmail_quote">2007/3/5, Adam Pridgen &lt;<a href="mailto:atpridgen@mail.utexas.edu" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">atpridgen@mail.utexas.edu</a>&gt;:</span>
<div><span class="e" id="q_1112287864bbda49_1"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
here&#39;s the magic you are looking for:<br><br>func_str = \<br>&#39;&#39;&#39;<br>def some_func(value):<br># youwould check value instance here and do something to it<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Hello World&quot;, value<br>&nbsp;&nbsp;&nbsp;&nbsp;return &quot;Done&quot;
<br>&#39;&#39;&#39;<br>exec(func_str)<br>f = locals()[&quot;some_func&quot;]<br>print f(&quot;wasn&#39;t that cool!&quot;)<br><br>When you exec the str, it will create a function object, and then you<br>can obtain the object by accessing the object by kwd in the locals()
<br>dictionary.&nbsp;&nbsp;Guess it&#39;s not really magic, but I think it is still<br>pretty cool ;)&nbsp;&nbsp;There also several variations to this method, but this<br>is the most readable IMHO.<br><br>Cheers,<br><br>Adam<br><br><br>On 3/2/07, Cecilia Alm &lt;
<a href="mailto:flickita@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">flickita@gmail.com</a>&gt; wrote:<br>&gt; I know that there are several ways to execute a string which represents a
<br>&gt; piece of python code.<br>&gt;&nbsp;&nbsp;Out of curiosity, is it only eval which returns a value? (as below, where
<br>&gt; the string corresponds to a defined function).<br>&gt;<br>&gt;&nbsp;&nbsp;&gt;&gt;&gt; def addone(val):<br>&gt;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp; return val + 1<br>&gt;&nbsp;&nbsp;...<br>&gt;&nbsp;&nbsp;&gt;&gt;&gt; res = eval(&#39;addone(10)&#39;)<br>&gt;<br>&gt; Thanks!
<br>&gt;<br>&gt;<br>&gt;<br>&gt; _______________________________________________<br>&gt; Tutor maillist&nbsp;&nbsp;-&nbsp;&nbsp;<a href="mailto:Tutor@python.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">Tutor@python.org
</a><br>&gt; <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://mail.python.org/mailman/listinfo/tutor</a><br>&gt;<br>&gt;<br></blockquote></span></div></div><span class="sg"><br><br clear="all"><br>-- <br>E. Cecilia Alm<br>Graduate student, Dept. of Linguistics, UIUC<br>Office: 2013 Beckman Institute
</span><br clear="all"><br>-- <br>E. Cecilia Alm<br>Graduate student, Dept. of Linguistics, UIUC<br>Office: 2013 Beckman Institute