Understanding memory location of Python variables

Dan Stromberg drsalists at gmail.com
Sun Jun 17 17:13:29 EDT 2018


On Sun, Jun 17, 2018 at 5:05 AM, Marko Rauhamaa <marko at pacujo.net> wrote:

> Bart <bc at freeuk.com>:
> > So, how /do/ you obtain the memory address of those values are
> > located? For example, in order to pass it to some foreign C function
> > that takes a void* parameter.
>
> That is dependent on the Python implementation. CPython supports native
> C and C++ extensions:
>
>    <URL: https://docs.python.org/3/extending/index.html>
>
> Here's the C API for extracting data out of strings:
>
>    <URL: https://docs.python.org/3/c-api/unicode.html#unicode-objects>
>

I'd actually recommend, in order from greatest preference to least:

1) Cython (but probably only for CPython)
2) CFFI (for CPython and Pypy, but perhaps not for others)
3) ctypes (but only for quick little projects with CPython or PyPy)
4) The new stable C extension type ABI (for CPython...  and PyPy?)
5) The old, common C extension type ABI (for CPython, and if you don't get
too fancy, for PyPy)

Cython is almost like Python, except it allows you to intermix C and Python
datatypes.  And it gets a lot of the silly fiddly bits right, so you don't
have to.  But you can still do byte-level stuff with it, if you actually
need to.



More information about the Python-list mailing list