Using Arrays with Python Ctypes

  Рет қаралды 4,466

CodersLegacy

CodersLegacy

Жыл бұрын

In this Video we will discuss how to pass Arrays back and forth between our C and Python programs using the ctypes library.
C File Compilation Command:
gcc -fPIC -shared -o clibrary.so clibrary.c
Website Link:
coderslegacy.com/python/ctype...
Related Links:
Ctypes Basics: • Python ctypes Tutorial...
Ctypes Datatypes and Strings: • Python ctypes Library ...
Ctypes Pointers: • Introducing Pointers i...
Ctypes with C++: • Ctypes Library - How t...

Пікірлер: 14
@AngelLilo
@AngelLilo 5 ай бұрын
This really supported me in my code. Ty soooo much!
@dimonlol7249
@dimonlol7249 3 ай бұрын
Thanks so much bro!
@coderslegacy5661
@coderslegacy5661 3 ай бұрын
Happy to help
@Popart-xh2fd
@Popart-xh2fd 3 ай бұрын
At 12:09 the *arr[10]* gets deleted when goes out of function scope, so that is undefined behaviour in C!
@kcvinu
@kcvinu Жыл бұрын
Thanks for the video.
@coderslegacy5661
@coderslegacy5661 Жыл бұрын
Glad you found it helpful. I recieved some positive feedback on the Ctypes videos I had made earlier, and people were requesting more videos on advanced Ctypes topics, hence I made this one on Arrays. There is another one on "Structs" and "Classes" releasing in 2-3 days, so stay tuned!
@kcvinu
@kcvinu Жыл бұрын
@@coderslegacy5661 Great!. Eagerly waiting for the video. Currently I am writing a gui library with win32 api funs. Your videos are a great help for me.
@bbjohn83
@bbjohn83 Жыл бұрын
First off, your tutorials are great and appreciate them. Thumbs up! I am running into an issue where I need to relaunch vscode every time edit and recompile the c function. Otherwise, my python terminal keeps showing the same outputs of the function before I edited + recompiled. Once I close and reopen vscode and run my py code, it shows the correct output reflecting the changes I made to the c function. I am following your example arraySum example exactly line-by-line. Any ideas how to fix this?
@Mzkysti
@Mzkysti Жыл бұрын
Hi, thanks for the video, I was just wondering why not do so called inplace "getArray" or is this possible with ctypes?: #include #include void getArray(int* arr) { for (int i = 0; i
@undeadpresident
@undeadpresident Жыл бұрын
That will work if you make the array in Python first. Sometimes you might have the array in the C code first though.
@coderslegacy5661
@coderslegacy5661 Жыл бұрын
@@undeadpresident Yup, that sounds correct to me.
@undeadpresident
@undeadpresident Жыл бұрын
You can pass numpy arrays and array module arrays too.
@coderslegacy5661
@coderslegacy5661 Жыл бұрын
Good to know.
@undeadpresident
@undeadpresident Жыл бұрын
@@coderslegacy5661 yeah with the numpy arrays the easiest way I've found is to pass (numpyarrayname.ctypes.data) to a ctypes function with c_void_p as the argtype. The other way I've used with numpy is to import ndpointer from numpy.ctypeslib then pass the array directly into the function which has the argtype (ndpointer(ctypes.c_int)) or w/e datatype. With the array module there's a command arrayname.buffer_info() which returns the array address in it's first index which can be passed to a function with c_void_p as argtype. I was just reading through the python documentation again, and it looks like they have an entire "buffer protocol" which can be used for converting arrays from C into Python objects, but I don't understand how to use it.
Using Structs and Classes with Python Ctypes
25:37
CodersLegacy
Рет қаралды 5 М.
Introducing Pointers in Python using the Ctypes Library
17:02
CodersLegacy
Рет қаралды 6 М.
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Паша Осадчий
Рет қаралды 6 МЛН
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 40 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 19 МЛН
Does size matter? BEACH EDITION
00:32
Mini Katana
Рет қаралды 20 МЛН
Python ctypes Library - Memory, Strings, Datatypes
16:47
CodersLegacy
Рет қаралды 8 М.
Extending Python With C
28:25
Christian Gould
Рет қаралды 26 М.
Python Standard Library: Functools
33:42
Jake Callahan
Рет қаралды 1,7 М.
Make Python code 1000x Faster with Numba
20:33
Jack of Some
Рет қаралды 441 М.
Python ctypes Tutorial - Using C/C++ Functions in Python
14:40
CodersLegacy
Рет қаралды 47 М.
10 Python Shortcuts You Need To Know
27:27
Tech With Tim
Рет қаралды 292 М.
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Паша Осадчий
Рет қаралды 6 МЛН