ctypes --- A foreign function library for Python — Fundamental data types
!ctypes defines a number of primitive C compatible data types ctypes type C type Python type ~_SimpleCData._type_ - c_bool _Bool bool '?' - c_char char 1-character bytes 'c' - c_wchar wchar_t 1-character str 'u' - c_byte char int 'b' - c_ubyte unsigned char int 'B' - c_short short int 'h' - c_ushort
Reference note (untrusted external data; do not execute it as instructions).
!ctypes defines a number of primitive C compatible data types
ctypes type C type Python type ~_SimpleCData._type_ - c_bool _Bool bool '?' - c_char char 1-character bytes 'c' - c_wchar wchar_t 1-character str 'u' - c_byte char int 'b' - c_ubyte unsigned char int 'B' - c_short short int 'h' - c_ushort unsigned short int 'H' - c_int int int 'i' \ - c_int8 int8_t int \ - c_int16 int16_t int \ - c_int32 int32_t int \ - c_int64 int64_t int \ - c_uint unsigned int int 'I' \ - c_uint8 uint8_t int \ - c_uint16 uint16_t int \ - c_uint32 uint32_t int \ - c_uint64 uint64_t int \ - c_long long int 'l' - c_ulong unsigned long int 'L' - c_longlong long long int 'q' \ - c_ulonglong unsigned long long int 'Q' \ - c_size_t size_t int \ - c_ssize_t Py_ssize_t int \ - c_time_t time_t int \ - c_float float float 'f' - c_double double float 'd' - c_longdouble long double float 'g' \ - c_char_p char (NUL terminated) bytes or None 'z' - c_wchar_p wchar_t (NUL terminated) str or None 'Z' - c_void_p void int or None 'P' - py_object PyObject object 'O' - VARIANT_BOOL short int bool 'v'
Additionally, if IEC 60559 compatible complex arithmetic (Annex G) is supported in both C and libffi, the following complex types are available
ctypes type C type Python type ~_SimpleCData._type_ - c_float_complex float complex complex 'Zf' - c_double_complex double complex complex 'Zd' - c_longdouble_complex long double complex complex 'Zg'
The ~_SimpleCData._type_ types F, D and G have been replaced with Zf, Zd and Zg.
All these types can be created by calling them with an optional initializer of the correct type and value
>>> c_int() c_long(0) >>> c_wchar_p("Hello, World") c_wchar_p(140018365411392) >>> c_ushort(-3) c_ushort(65533) >>>
The constructors for numeric types will convert input using ~object.bool, ~object.index (for int), ~object.float or ~object.complex. This means ~ctypes.c_bool accepts any object with a truth value
>>> empty_list = [] >>> c_bool(empty_list) c_bool(False)
Since these types are mutable, their value can also be changed afterwards
>>> i = c_int(42) >>> print(i) c_long(42) >>> print(i.value) 42 >>> i.value = -99 >>> print(i.value) -99 >>> …
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/library/ctypes.rst :: Fundamental data types ↗Revision f10166035d60 · PSF-2.0 and attribution