ctypes.
Structure
(*args, **kw)
Abstract base class for structures in native byte order.html
Concrete structure and union types must be created by subclassing one of these types, and at least define a _fields_
class variable. ctypes
will create descriptors which allow reading and writing the fields by direct attribute accesses. These are thepython
_fields_
A sequence defining the structure fields. The items must be 2-tuples or 3-tuples. The first item is the name of the field, the second item specifies the type of the field; it can be any ctypes data type.ide
For integer type fields like c_int
, a third optional item can be given. It must be a small positive integer defining the bit width of the field.ui
Field names must be unique within one structure or union. This is not checked, only one field can be accessed when names are repeated.this
It is possible to define the _fields_
class variable after the class statement that defines the Structure subclass, this allows creating data types that directly or indirectly reference themselves:spa
class List(Structure): pass List._fields_ = [("pnext", POINTER(List)), ... ]
The _fields_
class variable must, however, be defined before the type is first used (an instance is created, sizeof()
is called on it, and so on). Later assignments to the _fields_
class variable will raise an AttributeError.code
It is possible to defined sub-subclasses of structure types, they inherit the fields of the base class plus the _fields_
defined in the sub-subclass, if any.component