On Tue, Nov 2, 2010 at 4:36 PM, Brett Calcott <[email protected]> wrote:
> On 2 November 2010 17:17, Robert Bradshaw <[email protected]> 
> wrote:
>> On Sun, Oct 31, 2010 at 8:44 PM, Brett Calcott <[email protected]> 
>> wrote:
>>> 1. Why must I use a *pointer* in my python class?
>>> cdef class PythonVector2d:
>>>   cdef Vector2d *obj
>>> There is a default constructor, and I can happily declare it on the stack in
>>> a function. I'm sure I'm missing something here; is there some danger with
>>> allowing this?
>>
>> It's conceivable that we could allow "stack allocated" members in an
>> object in the presence default constructors, but that would require
>> modifying the constructor and destructor to manually initialize and
>> deconstruct the object when the PyObject* is allocated and reclaimed.
>
> I don't get why you need to manually do it. Doesn't the c++ default
> construction just do it for you?

No, because the memory for the class object is allocated deep withing
the Python library with an ordinary malloc, and the pointers just
passed around.

>>> 2. How hard would it be to automatically forward member access to a
>>> contained cppclass? Consider the following nasty hack:
>>> cdef class PythonVector2d:
>>>   cdef public float x, y
>>>   cdef Vector2d *obj
>>>   def __cinit__(self, x, y):
>>>     self.x = x
>>>     self.y = y
>>>     self.obj = <Vector 2d*>x
>>> ok. That is a bit nasty (I'm sure someone can tell me lots of reasons why I
>>> shouldn't do this).
>>
>> In reading that code, I'm not sure what your intent is.
>>
>
> Nastiness explained: Vector2d is a struct with 2 float members. I
> declare 2 float members in the python object, and then get a pointer
> to the first of these and cast it to a Vector2d. Assuming that the
> struct layout is the same in both of these, then accessing members via
> the cython properties will update the same shared bit of memory that
> the c++ struct uses.

If this is just for syntactic sugar, it sound way to messy... I could
see simplifications of the properties syntax to make Python access
easier.

On 2 November 2010 20:02, Stefan Behnel <[email protected]> wrote:
> Robert Bradshaw, 02.11.2010 07:17:
>> On Sun, Oct 31, 2010 at 8:44 PM, Brett Calcott wrote:
>>> cdef class PythonVector2d:
>>>    cdef Vector obj
>>>    properties:
>>>       public obj.x as x
>>>       public obj.y as y
<--- snip -->
> For this case, I'd prefer a version entirely without body, maybe even
>
>     property x = obj.x
>
> although the "as" version would also work. However, it's not so clear from
> the above that both setter and getter get generated, and how you would get
> only the getter for read-only access, for example. And why not have a
> "delattr" as well? May make sense in some cases.

setters, getters, (and delattrs?) would be defined iff they make sense
on the expression in question. One could imagine

    property readonly obj.x as x

or something like that. I'm not a fan of the = sign, as it's no where
else used for declarations (it's always a statement), unless we do
something like

    public x = property(obj.x)

which reads kind of like a default value.

- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to