instance as a sequence

Paul McGuire ptmcg at austin.rr.com
Mon Nov 5 12:40:24 EST 2007


On Nov 5, 11:32 am, snd... at gmail.com wrote:
> suppose i want to
> make foo.childNodes[bar] available as foo[bar]
> (while still providing access to the printxml/printprettyxml()
> functions
> and other functionality of dom/minidom instance).
>
> What is a good way to accomplish that?

define __getitem__(self, index) method on foo's class, and have the
method return foo.childNodes[index].

If you don't have access to foo's class, then just do this:

foo.__getitem__ = delegatingGetItemMethod

and define delegatingGetItemMethod as described above.

-- Paul




More information about the Python-list mailing list