Using Structs and Classes with Python Ctypes

Поделиться
HTML-код
  • Опубликовано: 5 сен 2024

Комментарии • 18

  • @rajeshpuppala7671
    @rajeshpuppala7671 Год назад +1

    Most underrated channel

  • @ReinventedWeb
    @ReinventedWeb Год назад +1

    Great video as always. Keep it up!

  • @cmfanz
    @cmfanz Год назад

    This video series is a treasure trove. Really appreciate your efforts in making this. While I have only gone through the first couple of videos, I wanted to know if cython is any better than ctype. Why do certain people seem to prefer cython more than ctypes? Are there any demerits to it? Also I notice that you compile the shared library file as a .so file. My recollection of a .so file is it is a linux equivalent of a .dll windows file (dynamically linked library file at runtime). So in your case, is the .so identical to what a .dll is?

    • @coderslegacy5661
      @coderslegacy5661  Год назад +1

      Appreciate the feedback. Glad to hear people are finding it useful.
      .so and .dll are pretty similar in their purpose. Obviously there are some implementation differences, but the idea is the same.
      As to your other question, Cython is a more "pythonic" way of doing things. Not alot of people are going to be comfortable writing code in C/C++. Similarly, alot of people would simply prefer having everything in a single file. Cython allows you to modify existing code pretty easily too.
      Ctypes has its benefits of course. Cython converts Python code to C/C++. Ctypes directly uses C/C++ code, which removes conversion overhead. Likewise, there may be existing C/C++ code which you want to use in a Python application, or a specific library which is only available in C/C++.
      It's a pretty opiniated topic, as even the performance is similar. Comes down to preference, and what task you are using it for.

    • @coderslegacy5661
      @coderslegacy5661  Год назад

      Just an FYI for you, and any other future readers. I did a little more research into Ctypes and Python, and even ran some performance benchmarks myself. Cython is faster than Ctypes by a good margin. This is because Ctypes is designed primarily for "compatibility" with C/C++ rather than speed.
      Using Ctypes for a performance boost is still a viable option though. I have a video on that coming out in a few days. Will link it here when it comes out.
      (Edit) Here it is: ruclips.net/video/yzgFMoZiRNw/видео.html

    • @cmfanz
      @cmfanz Год назад

      @@coderslegacy5661 wow that's going to be a mouth watering video. I am looking forward to it. If you can do a cython playlist series for beginners, that would be the icing on the cake. I am open to learning both ctypes and cython and using them as situation demands.

  • @luisacoelho8969
    @luisacoelho8969 Год назад

    instead of printing the values from the structures in PointArray, how can i store them in the python code, so that i can use the values through out that exact python code?

  • @cmfanz
    @cmfanz Год назад +1

    I tried the exact same code specified in the Example #2 where you return a struct via the C file to be used within the python file. Everything is identical. But I get garbage values for x and y instead of 10 and 20.

    • @coderslegacy5661
      @coderslegacy5661  Год назад

      That's strange. Can you try using the website link in the description and copy pasting the code from there? (or use it as reference). Even a slight mistake can result in getting undefined values (i know from experience).

    • @cmfanz
      @cmfanz Год назад

      @@coderslegacy5661 I tried now to copy paste your code from the website. The python results are coming out as 0 and 0 now for x and y instead of 50 and 10 as expected from the c struct.

    • @cmfanz
      @cmfanz Год назад +1

      found out what the issue is. I have to dynamically allocate memory to the struct in the function that contains a pointer to the struct. And then I can free the memory to the stuct in the free_mem C function.

    • @coderslegacy5661
      @coderslegacy5661  Год назад +1

      That's a bit strange. I'll look into this a bit to try and understand why that happened. When I tried it, I only had to declare it explicitly (with malloc) when I was dealing with pointers.
      Glad to see you solved your issue though.

    • @cmfanz
      @cmfanz Год назад +3

      @@coderslegacy5661 As far as I could see, Example #2 in the video did not specify needing to use malloc while using pointer to struct in the c file. The emphasis for using malloc was specified only at the time when we moved farther down into the video when we were working with C structs having string data types that needed to be returned to python. I think that is where I might have been confused a bit. However, I am glad this issue is fixed now. I will be using malloc by default henceforth whenever I am using a pointer to a struct regardless of the datatypes of the struct member variables.

  • @sebastiansuper3658
    @sebastiansuper3658 Год назад

    Hello, i love your videos! it would be impossible for me to explore ctypes without your tutorials! I have one issue though: Is there a way to save the values of a reutrned pointer? My C-function returns a ctypes.POINTER(ctypes.c_float) and i am able to access those values with array[i], however i want to be able to be using this array as a list in python even after i freed the memory..

    • @coderslegacy5661
      @coderslegacy5661  Год назад +1

      You can always use .contents attribute to access what a pointer points to. And .value attribute to retrieve a pythonic datatype from a ctype datatype.
      Using these you can save the values in a Python list. (Imagine iterating over the ctype array and pushing each element to a Python list)
      Another technique which might be possible is to use a list() constructor. I haven't tried it myself though.

    • @sebastiansuper3658
      @sebastiansuper3658 Год назад +1

      @@coderslegacy5661 Thank you for your answer! I really appreciate it :) I solved it with the slice (list = output[:length]) operator

  • @imveryhungry112
    @imveryhungry112 8 месяцев назад

    I wish I was smarter so I could understand :(