[Tutor] Query about getattr used as a dispatcher
Chris Calloway
cbc at unc.edu
Wed Jan 17 15:41:32 CET 2007
raghu raghu wrote:
> Actually i installed python 2.5 i ran this script and its showing error
> it could not import statsout. why is it so?
statsout is a *hypothetical* module used for an example only. The
statsout module does not actually exist. When Dive Into Python wants you
to type in an example, it will shown either as lines at the Python
interpreter prompt (>>>), or it will be an example in a Python source
file included with the Dive Into Python examples bundle. If you look at
the example using the imaginary statsout module in section 4.12, which
is not included in any Python source file in the Dive Into Python
examples bundle, you will see the example is not referenced in a file,
nor is it shown as being typed at the Python interpreter prompt.
If, however, you had an actual statsout module in your sys.path, you
could import it. And it that module had top level functions functions
that took one argument and had function names like "output_text" and
"output_pdf" and "output_html," then the example would work if you typed
it in. The example is just showing a hypothetical case of a very simple
dispatcher. The example is asking you to imagine *if* you had a statsout
module, and *if* that module had functions by those names in it.
Dive Into Python uses the getattr function in the apihelper.py example
you are currently reading about. In the next chapter, a more complicated
example is shown where getattr is used in a dispatcher which finds, not
just a function by name, but a class by name and dispatches that class
object to create a new object of that class. So the statsout *imaginary*
example is just preparing you for a more complicated *real life*
dispatcher example in the next chapter. Hypothetical examples are often
shown in programming books to prepare you for more complicated real life
examples.
--
Sincerely,
Chris Calloway
http://www.seacoos.org
office: 332 Chapman Hall phone: (919) 962-4323
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599
More information about the Tutor
mailing list