<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 8/4/2010 1:23 PM, Pete wrote:
<blockquote cite="mid:3502A2E1-994D-47E4-BA7E-28C5AF06348E@xs4all.nl"
 type="cite">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 moz-do-not-send="true"
 href="http://diveintopython.org/power_of_introspection/index.html">http://diveintopython.org/power_of_introspection/index.html</a></div>
  <div><br>
  </div>
  <div><span class="Apple-style-span"
 style="font-family: 'Book Antiqua',Georgia,Palatino,Times,'Times New Roman',serif; color: rgb(34, 34, 34); line-height: 23px;">
  <pre class="programlisting" style="margin: 1em 1em 0px; padding: 0px;"><span
 class="pykeyword"
 style="background-color: white; font-weight: bold; color: navy;">import</span> statsout

<span class="pykeyword"
 style="background-color: white; font-weight: bold; color: navy;">def</span><span
 class="pyclass"
 style="background-color: white; font-weight: bold; color: blue;"> output</span>(data, format=<span
 class="pystring" style="background-color: white; color: olive;">"text"</span>):
    output_function = getattr(statsout, <span class="pystring"
 style="background-color: white; color: olive;">"output_%s"</span> % format, statsout.output_text)
    <span class="pykeyword"
 style="background-color: white; font-weight: bold; color: navy;">return</span> output_function(data) </pre>
  <pre class="programlisting" style="margin: 1em 1em 0px; padding: 0px;">
  </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. </div>
  </div>
</blockquote>
<br>
Everything in Python is in a module. I suspect you mean not in an
imported module, e.g. in the main module. Whose __name__ is '__main__'.
A reference to the main module can be found:<br>
<br>
import sys<br>
main = sys.modules['__main__']<br>
<br>
<blockquote cite="mid:3502A2E1-994D-47E4-BA7E-28C5AF06348E@xs4all.nl"
 type="cite">
  <div>
  <div>Is that possible?</div>
  </div>
  <div><br>
  </div>
  <div>Something like this:</div>
  <div><br>
  </div>
  <div><span class="Apple-style-span"
 style="color: rgb(34, 34, 34); font-family: 'Book Antiqua',Georgia,Palatino,Times,'Times New Roman',serif; line-height: 23px;">
  <pre class="programlisting" style="margin: 1em 1em 0px; padding: 0px;"><span
 class="pykeyword"
 style="background-color: white; font-weight: bold; color: navy;">def</span><span
 class="pyclass"
 style="background-color: white; font-weight: bold; color: blue;"> output_text</span>(data):</pre>
  <pre class="programlisting" style="margin: 1em 1em 0px; padding: 0px;">    return_value = "This is text: " + data
    <span class="pykeyword"
 style="background-color: white; font-weight: bold; color: navy;">return</span> return_value</pre>
  </span></div>
  <div><span class="Apple-style-span"
 style="color: rgb(34, 34, 34); font-family: 'Book Antiqua',Georgia,Palatino,Times,'Times New Roman',serif; line-height: 23px;">
  <pre class="programlisting" style="margin: 1em 1em 0px; padding: 0px;"><span
 class="pykeyword"
 style="background-color: white; font-weight: bold; color: navy;">
</span></pre>
  <pre class="programlisting" style="margin: 1em 1em 0px; padding: 0px;"><span
 class="pykeyword"
 style="background-color: white; font-weight: bold; color: navy;">def</span><span
 class="pyclass"
 style="background-color: white; font-weight: bold; color: blue;"> output</span>(data, format=<span
 class="pystring" style="background-color: white; color: olive;">"text"</span>):
    output_function = getattr(*****, <span class="pystring"
 style="background-color: white; color: olive;">"output_%s"</span> % format, statsout.output_text)
    <span class="pykeyword"
 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>
</blockquote>
<br>
Replace those stars with main as derived above.<br>
<br>
globals()['output_text'] will also give you a reference to the function.<br>
<br>
<pre class="moz-signature" cols="72">-- 
Bob Gailer
919-636-4239
Chapel Hill NC</pre>
</body>
</html>