C API []-style access to instance objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • williams.jasonscott@gmail.com

    #1

    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:
    [color=blue][color=green][color=darkred]
    >>> obj = foo.Foo()
    >>> obj["key"][/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unsubscriptable object[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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~

  • André Malo

    #2
    Re: C API []-style access to instance objects

    * williams.jasons cott@gmail.com wrote:
    [color=blue]
    > 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:
    >[color=green][color=darkred]
    >>>> obj = foo.Foo()
    >>>> obj["key"][/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: unsubscriptable object[color=green][color=darkred]
    >>>>[/color][/color]
    >
    > 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?[/color]

    tp_as_mapping is what you're looking for. It takes the pointer to another
    structure: PyMappingMethod s.

    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
    --
    "Umfassende s Werk (auch fuer Umsteiger vom Apache 1.3)"
    -- aus einer Rezension

    <http://pub.perlig.de/books.html#apac he2>

    Comment

    • williams.jasonscott@gmail.com

      #3
      Re: C API []-style access to instance objects

      That worked! Thank You!

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

      thanks....
      ~jason

      Comment

      Working...