0

This is probably a question about "C" not "Cython" but I am trying to understand how classes work in cython.

When I write a class in Cython:

cdef class Myclass:

    def __init__(self, a1, ...):
        self.attribute1 = a1

    def __cinit__(self, char *ca1,...):
        self.c_attribute1 = Ca1
        ...

I understand that it's basically a struct in c.

But then:

cdef muFunction(self):
    do and return something

How are the methods private/local to the object? Can then run at C compiled speed?

Thank you and feel free to point and teach me all terms that I might be using wrongly.

DavidW
  • 29,336
  • 6
  • 55
  • 86
aerijman
  • 2,522
  • 1
  • 22
  • 32
  • Profile and assess. – jxh Dec 04 '20 at 19:37
  • @jxh what do I compare to? to a C implementation? I changed the title. My question is more about how these functions are methods in C. – aerijman Dec 04 '20 at 19:40
  • You compare the Cython based code with what you think the equivalent C would have been without Cython. – jxh Dec 04 '20 at 19:44
  • 1
    It's probably worth looking at https://stackoverflow.com/questions/28362009/definition-of-def-cdef-and-cpdef-in-cython/41976772#41976772 - almost all of it applies to methods too. They key point is that `cdef` vs `def` is mainly calling convention, but the code inside runs at exactly the same speed. Beyond that you can probably get some understanding by looking at the generated C code (either the annotated html or search for your source lines in the .c) – DavidW Dec 04 '20 at 19:46
  • @jxh Thank you. I will try and I like the pragmatic approach. I would still be glad if you/someone could give me a hint on how do you make these functions "local" to the struct (am I using the term local correctly?). – aerijman Dec 04 '20 at 19:47
  • 1
    @ead has [a nice answer](https://stackoverflow.com/a/48879057/4657412) about the fine details of how methods are implemented (which is probably more than you need to know) – DavidW Dec 04 '20 at 19:51
  • Can't you find the resulting `c` code to examine? I believe 'compiling' cython has an annotation parameter that creates a `html` file with highlighting. Because the C code is machine generated it isn't pretty. There's a lot of boilerplate. – hpaulj Dec 04 '20 at 21:37

0 Answers0