embedding python: simulating sequence get item / set item methods

J

Johannes Zellner

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?
 
A

Alex Martelli

Johannes Zellner said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top