C API []-style access to instance objects
André Malo
auch-ich-m at g-kein-spam.com
Thu Apr 27 02:11:56 EDT 2006
* williams.jasonscott at gmail.com wrote:
> Hi,
>
> I am having trouble figuring out how to set up an object to be
> scriptable through the C API. An example of what I am attempting to
> do:
>
>>>> obj = foo.Foo()
>>>> obj["key"]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: unsubscriptable object
>>>>
>
> I've tried defining __getitem__ as a method, but that hasn't worked.
> Is there a tp_field that I am failing to understand, something like
> tp_iter and tp_iternext?
tp_as_mapping is what you're looking for. It takes the pointer to another
structure: PyMappingMethods.
You might want to try the modulator script (comes with the source
distribution of python), which generates some of the basic type methods for
you. It's a bit old, but gives a good start.
nd
--
"Umfassendes Werk (auch fuer Umsteiger vom Apache 1.3)"
-- aus einer Rezension
<http://pub.perlig.de/books.html#apache2>
More information about the Python-list
mailing list