470,594 Members | 1,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

C API []-style access to instance objects

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?

thanks~

Apr 26 '06 #1
2 984
* wi*****************@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>
Apr 27 '06 #2
That worked! Thank You!

I'd also like to say this group is great at fast accurate responses!
Cheers!

thanks....
~jason

Apr 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by MikeAth via DotNetMonster.com | last post: by
2 posts views Thread by Kurt Schroeder | last post: by
4 posts views Thread by VK | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.