embedding python: simulating sequence get item / set item methods

Discussion in 'Python' started by Johannes Zellner, Jan 17, 2006.

  1. 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 = value

    and the second method, if accessing the list

    print str(my_list)

    Is this possible?

    --
    Johannes
     
    Johannes Zellner, Jan 17, 2006
    #1
    1. Advertising

  2. Johannes Zellner <> wrote:

    > 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 = value
    >
    > and the second method, if accessing the list
    >
    > print str(my_list)
    >
    > Is this possible?


    Sure, see the details at <http://docs.python.org/api/newTypes.html> and
    consider that what you want will need to go into a PyMappingMethods or
    PySequenceMethods 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
     
    Alex Martelli, Jan 17, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cppaddict
    Replies:
    12
    Views:
    2,224
    Andy Venikov
    May 26, 2004
  2. jpr
    Replies:
    2
    Views:
    337
    red floyd
    Mar 25, 2006
  3. stef mientki
    Replies:
    13
    Views:
    644
    stef mientki
    Oct 20, 2007
  4. bilgekhan
    Replies:
    18
    Views:
    493
    bilgekhan
    May 28, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    320
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page