3

If I have the struct

cdef struct Interval:
    unsigned int start
    unsigned int end
    unsigned int index

I can assign values to it like

i.start = 1

but can I set all the values (start, end, index) in one go?

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156

1 Answers1

4

I couldn't actually find this in the documentation, but cython does support the equivalent of struct initialization in c

%%cython
def f():
    cdef Interval i = [1, 1, 3]
    return i.index

c code generates to:

  struct __pyx_t_46_cython_magic_f52bf70efc56b7361a3a2e15f913f262_Interval __pyx_t_1;

  /* "_cython_magic_f52bf70efc56b7361a3a2e15f913f262.pyx":14
 * 
 * def f():
 *     cdef Interval i = [1, 1, 3]             # <<<<<<<<<<<<<<
 *     return i.index
 */
  __pyx_t_1.start = 1;
  __pyx_t_1.end = 1;
  __pyx_t_1.index = 3;
chrisb
  • 49,833
  • 8
  • 70
  • 70