Niko Skrypnik, 28.06.2012 09:31:
> суббота, 26 мая 2012 г., 1:56:41 UTC+3 пользователь Robert Bradshaw написал:
>> On Fri, May 25, 2012 at 3:52 PM, VictorNorman wrote:
>>> I have a Cython extension type ResProperties:
>>>
>>> cdef class ResProperties:
>>> cdef public float endDayUtilities
>>> cdef public list marginalUtilities
>>> cdef public int held
>>> ... etc, etc. ...
>>>
>>> and I have a Cython extension type Agent:
>>>
>>> cdef class Agent(object):
>>> cdef public ResProperties[c.NUM_RESOURCES] resProp
>>>
>>> In my current implementation the resProp is a python list of
>>> ResProperties
>>> objects, always of length c.NUM_RESOURCES. I'd like to make that more
>>> efficient by declaring it to be an array of ResProperties objects.
I should reiterate that this is not necessarily making it more efficient.
It depends on what the code actually does.
>>> Is there a way to do this? Is there a web page / tutorial where I
>>> should have found this info myself?
>>
>> No, there's currently no way to do this. Note, however, that getting
>> from/setting to a Python list is already rather efficient (it indexes
>> into the underlying PyObject*).
>>
>> As I understand it's also impossible now to do something like
>
> cdef class Agent(object):
> cdef ResProperties* resProp
>
> i.e. to make pointer of extension type.
Correct, although you can take a generic <void*> or <PyObject*> from a
Python object. It just doesn't have attributes or methods.
> Or I'm wrong? For me it was clear
> that it possible cause as I got it from docs and generated C files,
> extension type becomes a C struct after translation so I thought that I can
> do this way. But in fact I've get error from cython translator - "Pointer
> base type cannot be a Python object". Hm, why is this Python object rather
> than C struct?
It's not that simple. Object references are reference counted. Pointers are
not. Language support for borrowed (i.e. non reference counted) references
is still not implemented due to lack of interest and/or sponsorship.
However, borrowed references are a rather tricky thing to use anyway. For
one, they barely work in PyPy, but also in CPython, you have to make sure
there actually is an owned reference to them somewhere as long as you make
use of them. That leads to duplication that can make code hard to
understand at times and introduce subtle bugs that only show coincidentally
way after writing the code.
Stefan