[PyQt] Sip iterator question
Sébastien Petitdemange
sebastien.petitdemange at esrf.fr
Mon Aug 31 17:16:25 BST 2009
Giovanni Bajo wrote:
> On 8/31/2009 4:24 PM, Sébastien Petitdemange wrote:
>> David Boddie wrote:
>>> On Mon, 24 Aug 2009 13:08:44 +0200, Sebastien Petitdemange wrote:
>>>
>>>> Sébastien Petitdemange wrote:
>>>>> I try to create an iterator using python iterator protocol but I
>>>>> didn't
>>>>> succeed.
>>> [...]
>>>
>>>>> Data_HeaderContainer_itemIterator* __iter__();
>>>>> %MethodCode
>>>>> sipRes = sipCpp;
>>>>> %End
>>>>> };
>>>>>
>>>>> and we I use it in python, It say that my object is not an iterator :
>>>>>
>>>>> for k in i:
>>>>> ... print k
>>>>> ...
>>>>> Traceback (most recent call last):
>>>>> File "<stdin>", line 1, in <module>
>>>>> TypeError: 'Data_HeaderContainer_itemIterator' object is not iterable
>>>>>
>>>>> what I'm I missing?
>>> Maybe the value returned by __iter__() isn't what the interpreter is
>>> expecting. Having said that, I think it's possible that the type
>>> structure
>>> information created by SIP doesn't have the necessary fields filled
>>> in to
>>> allow the object to be used to create an iterator, even if you define
>>> __iter__() for this purpose.
>>>
>>> Has anyone else encountered this problem, perhaps with tools other
>>> than SIP?
>>>
>>> David
>>>
>> As a conclusion, to create an iterator in C for python, you have to
>> define tp_iter slot and it seems that SIP don't yet recognize __iter__
>> as a slot that why it doesn't work.
>>
>> Hope this feature will be add to SIP!
>>
>> Thanks for your replay.
>
> Remember that many containers are already iterable just because they
> support operator[] (aka __getitem__). So if you have a container, it's
> sufficient to expose __getitem__ (which is probably a good idea anyway)
> to make it iterable.
Yes you right it a good idea, but I can only expose operator[](const
char*) because my container is a map<string,string>.
So It wont solve the for loop.
I already wrap items,values and keys to be compliant with python dict
but as you know those methods do a copy and It's not always a good idea
but I'll use them for now.
Hope one day sip add this feature ;)
Thanks a lot for you reply,
SEB
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sebastien_petitdemange.vcf
Type: text/x-vcard
Size: 210 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090831/edbee542/sebastien_petitdemange-0001.vcf
More information about the PyQt
mailing list