Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 17th, 2006, 12:55 AM
Johannes Zellner
Guest
 
Posts: n/a
Default 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
  #2  
Old January 17th, 2006, 01:25 AM
Alex Martelli
Guest
 
Posts: n/a
Default Re: embedding python: simulating sequence get item / set item methods

Johannes Zellner <johannes@zellner.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 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles