embedding python: simulating sequence get item / set item methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johannes Zellner

    #1

    embedding python: simulating sequence get item / set item methods

    Hello,

    when embedding python: how can I create a type which simulates item
    getters and setters? Something like this:

    void setter(PyObject * self, int i, PyObject new_value)
    {
    // do something
    }

    PyObject* getter(PyObject * self, int i)
    {
    // do something
    return something;
    }

    and I'd like the first method called if (from a python script):

    my_list[i] = value

    and the second method, if accessing the list

    print str(my_list[i])

    Is this possible?

    --
    Johannes
  • Alex Martelli

    #2
    Re: embedding python: simulating sequence get item / set item methods

    Johannes Zellner <johannes@zelln er.org> wrote:
    [color=blue]
    > Hello,
    >
    > when embedding python: how can I create a type which simulates item
    > getters and setters? Something like this:
    >
    > void setter(PyObject * self, int i, PyObject new_value)
    > {
    > // do something
    > }
    >
    > PyObject* getter(PyObject * self, int i)
    > {
    > // do something
    > return something;
    > }
    >
    > and I'd like the first method called if (from a python script):
    >
    > my_list[i] = value
    >
    > and the second method, if accessing the list
    >
    > print str(my_list[i])
    >
    > Is this possible?[/color]

    Sure, see the details at <http://docs.python.org/api/newTypes.html> and
    consider that what you want will need to go into a PyMappingMethod s or
    PySequenceMetho ds structure -- not well documented online, but just take
    a look in the Python sources at the way the type objects for lists &c
    are defined, and take it from there.


    Alex

    Comment

    Working...