How do I access what's in this module?

John Machin sjmachin at lexicon.net
Thu Jan 7 22:40:26 EST 2010


On Jan 8, 12:21 pm, Fencer <no.i.d... at want.mail.from.spammers.com>
wrote:
> Hello, look at this lxml documentation page:http://codespeak.net/lxml/api/index.html

That's for getting details about an object once you know what object
you need to use to do what. In the meantime, consider reading the
tutorial and executing some of the examples:
http://codespeak.net/lxml/tutorial.html

> How do I access the functions and variables listed?
>
> I tried from lxml.etree import ElementTree and the import itself seems
> to pass without complaint by the python interpreter but I can't seem to
> access anything in ElementTree, not the functions or variables. What is
> the proper way to import that module?
>
> For example:
>  >>> from lxml.etree import ElementTree
>  >>> ElementTree.dump(None)
> Traceback (most recent call last):
>    File "<console>", line 1, in <module>

lxml.etree is a module. ElementTree is effectively a class. The error
message that you omitted to show us might have given you a clue.

To save keystrokes you may like to try
    from lxml import etree as ET
and thereafter refer to the module as "ET"

| >>> from lxml import etree as ET
| >>> type(ET)
| <type 'module'>
| >>> type(ET.ElementTree)
| <type 'builtin_function_or_method'>
| >>> help(ET.ElementTree)
| Help on built-in function ElementTree in module lxml.etree:
|
| ElementTree(...)
|     ElementTree(element=None, file=None, parser=None)
|
|     ElementTree wrapper class.

> Also, can I access those items that begin with an underscore if I get
> the import sorted?

Using pommy slang like "sorted" in an IT context has the potential to
confuse your transatlantic correspondents :-)

Can access? Yes. Should access? The usual Python convention is that an
object whose name begins with an underscore should be accessed only
via a documented interface (or, at your own risk, if you think you
know what you are doing).

HTH,
John



More information about the Python-list mailing list