<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hey Huy,<div><br></div><div>thanks. But what is the first parameter in this case? (Note there is nothing here I'm trying to accomplish except understand).</div><div><br></div><div>In the original example, 'statsout' is the name of the module. In this case there is no module so why can you substitute 'output_text'?</div><div><br></div><div>thanks,</div><div><br></div><div>Pete</div><div><br><div><div>On 2010-08-04, at 3:09 PM, Huy Ton That wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>If it is in the same code, and the namespace isn't a module or another class you could do this:</div><div><br></div><div>def output_text(data):</div><div>&nbsp;&nbsp; &nbsp;return_value = "This is text: " + data</div><div>

&nbsp;&nbsp; &nbsp;return return_value</div><div><br></div><div>def output(data, format="text"):</div><div>&nbsp;&nbsp; &nbsp;output_function = getattr(output_text, "output_%s" % format, output_text)</div><div>&nbsp;&nbsp; &nbsp;return output_function(data)</div>

<div><br></div><div>-HTH</div><div><br></div><div>Huy</div><br><div class="gmail_quote">On Wed, Aug 4, 2010 at 1:23 PM, Pete <span dir="ltr">&lt;<a href="mailto:pkoek11@xs4all.nl">pkoek11@xs4all.nl</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div style="word-wrap:break-word">Hi,<div><br></div><div>I'm trying to understand the syntax for reflection in python. I was wondering about this.</div>

<div><br></div><div>From the example on diveintopython:</div><div><br></div><div>&nbsp;&nbsp; <a href="http://diveintopython.org/power_of_introspection/index.html" target="_blank">http://diveintopython.org/power_of_introspection/index.html</a></div>

<div><br></div><div><span style="font-family:'Book Antiqua', Georgia, Palatino, Times, 'Times New Roman', serif;color:rgb(34, 34, 34);line-height:23px"><pre style="margin-top:1em;margin-right:1em;margin-bottom:0px;margin-left:1em;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="background-color:white;font-weight:bold;color:navy">import</span> statsout

<span style="background-color:white;font-weight:bold;color:navy">def</span><span style="background-color:white;font-weight:bold;color:blue"> output</span>(data, format=<span style="background-color:white;color:olive">"text"</span>):
    output_function = getattr(statsout, <span style="background-color:white;color:olive">"output_%s"</span> % format, statsout.output_text)
    <span style="background-color:white;font-weight:bold;color:navy">return</span> output_function(data) </pre><pre style="margin-top:1em;margin-right:1em;margin-bottom:0px;margin-left:1em;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<br></pre></span><div>I was wondering about how to make this work if the function that you are trying to call is not in a module. Is that possible?</div></div><div><br></div><div>Something like this:</div><div><br></div>
<div>
<span style="color:rgb(34, 34, 34);font-family:'Book Antiqua', Georgia, Palatino, Times, 'Times New Roman', serif;line-height:23px"><pre style="margin-top:1em;margin-right:1em;margin-bottom:0px;margin-left:1em;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="background-color:white;font-weight:bold;color:navy">def</span><span style="background-color:white;font-weight:bold;color:blue"> output_text</span>(data):</pre><pre style="margin-top:1em;margin-right:1em;margin-bottom:0px;margin-left:1em;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
    return_value = "This is text: " + data
    <span style="background-color:white;font-weight:bold;color:navy">return</span> return_value</pre></span></div><div><span style="color:rgb(34, 34, 34);font-family:'Book Antiqua', Georgia, Palatino, Times, 'Times New Roman', serif;line-height:23px"><pre style="margin-top:1em;margin-right:1em;margin-bottom:0px;margin-left:1em;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="background-color:white;font-weight:bold;color:navy"><br></span></pre><pre style="margin-top:1em;margin-right:1em;margin-bottom:0px;margin-left:1em;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="background-color:white;font-weight:bold;color:navy">def</span><span style="background-color:white;font-weight:bold;color:blue"> output</span>(data, format=<span style="background-color:white;color:olive">"text"</span>):
    output_function = getattr(*****, <span style="background-color:white;color:olive">"output_%s"</span> % format, statsout.output_text)
    <span style="background-color:white;font-weight:bold;color:navy">return</span> output_function(data)</pre></span><div><br></div></div><div>What I can't figure out is what to put in place of *****. I've tried globals()['__name__'], in various forms, to no avail.</div>

<div><br></div><div>Any pointers would be appreciated.</div><div><br></div><div>thanks,</div><div><br></div><font color="#888888"><div>Pete</div><div><br></div></font></div><br>_______________________________________________<br>


Tutor maillist &nbsp;- &nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br>
</blockquote></div><br></div></body></html>