473,480 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to make an extension class have a __getitem__ interface?

I'm in the process of writing a few extension types, and there's one
that I'd sort of like to have getitem, setitem, getslice, setslice
functionality for.

I've been looking through the docs and how things are done in
Objects/listobject.c, and it appears tha there are a couple of ways to
do it and I'd like to get some opinions on what's the best way.

As I see it, here are my options (please correct me if I'm wrong):

1) Register my getitem handler as "__getitem__", my setitem handler as
"__setitem__", etc with my PyMethodDef struct for my new type.

2) Give my new type a full-blown sequence interface and stick my
handlers in the PySequenceMethods struct.

3) Give my new type a full-blown mapping interface and stick my
handlers in the PyMappingMethods struct.

I kind of want to shy away from (2) and (3) simply because there's
still a lot of stuff going on inside those Py(Sequence|Mapping)Methods
structs that I don't understand and I don't want to be responsible for
learning right now. But I'm also not sure if it's just as easy as
setting some functions to respond to __getitem__, __getslice__, etc.

As I'm still new to the world of writing extension modules, any
corrections or clarifications would be greatly appreciated. I've
mostly been using SWIG up to this point, but I've got some code here
where I really care what the Python interface looks like, and SWIG
just won't do what I want it to do (with nested templated classes and
the like).

Many thanks in advance.

--
Steve Juranich
Tucson, AZ
USA
Jul 18 '05 #1
1 2111

Steve Juranich wrote:
I'm in the process of writing a few extension types, and there's one
that I'd sort of like to have getitem, setitem, getslice, setslice
functionality for.

I've been looking through the docs and how things are done in
Objects/listobject.c, and it appears tha there are a couple of ways to do it and I'd like to get some opinions on what's the best way.

As I see it, here are my options (please correct me if I'm wrong):

1) Register my getitem handler as "__getitem__", my setitem handler as "__setitem__", etc with my PyMethodDef struct for my new type.

2) Give my new type a full-blown sequence interface and stick my
handlers in the PySequenceMethods struct.

3) Give my new type a full-blown mapping interface and stick my
handlers in the PyMappingMethods struct.

I kind of want to shy away from (2) and (3) simply because there's
still a lot of stuff going on inside those Py(Sequence|Mapping)Methods structs that I don't understand and I don't want to be responsible for learning right now. But I'm also not sure if it's just as easy as
setting some functions to respond to __getitem__, __getslice__, etc.

As I'm still new to the world of writing extension modules, any
corrections or clarifications would be greatly appreciated. I've
mostly been using SWIG up to this point, but I've got some code here
where I really care what the Python interface looks like, and SWIG
just won't do what I want it to do (with nested templated classes and
the like).


1. Define the interface you want; it doesn't have to be a full blown
sequence or a full blown mapping -- the main thing is that you know
what you want it to do; like what is the input to __getitem__, an index
or a key??

2. Implement it in Python. Write some code that will actually use it.
Refine your interface.

3. Make the minimal changes so that it can be compiled by pyrex. Read
the C code generated by pyrex. At this stage a masochist woulld say "Oh
goodie I know what to do now" and go off and hand code a C extension. A
sensible person would send Greg Ewing an appreciative e-mail message
and keep the extension in pyrex.

HTH,

John

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2664
by: Peter Sparago | last post by:
(Sorry in advance for the long post.) Hi, I'm having a great deal of difficulty buiding a Python COM extension. I am using the MSHTML ActiveX control in my application but I need to interact...
2
1749
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
3
5937
by: Tobiah | last post by:
#!/usr/bin/python # Hi, # # I noticed something interesting when trying to define # the __getitem__() method in a class that inherits from # (dict). If within the __getitem__ method I attempt...
12
1513
by: sandravandale | last post by:
It's important that I can read the contents of the dict without flagging it as modified, but I want it to set the flag the moment I add a new element or alter an existing one (the values in the...
28
2904
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
2671
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
2
1648
by: Samuel Siren | last post by:
Have just read about LINQ ant the new planned features of C# 3.0. I think it's fantastic, but I have a few questions about extension methods: Question 1: Syntax ------------------ Why is the...
0
1513
by: MarkE | last post by:
I'm just getting started on Boost Python and may have missed this obvious looking problem somewhere. Given a c-extension "testext" written using Boost Python containing a base class "Base", a...
1
1679
by: Simon Woods | last post by:
Hi I have 2 classes which are both instantiated by shared factory methods and both implement a common interface. Public Class Factory Public Shared Function CreateMyClass1() As MyClass1...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.