[plum.structure] Tutorial: Access Structure MembersΒΆ

This tutorial shows how to access structure members of a structure instance returned by the unpack() utility or by direct instantiation.

First create a custom structure type and instantiate it:

>>> from plum.structure import Structure, Member
>>> from plum.int.little import UInt8, UInt16
>>>
>>> class MyStruct(Structure):
...     m1: int = Member(cls=UInt8)
...     m2: int = Member(cls=UInt16)
...
>>> mystruct = MyStruct(m1=1, m2=2)
>>> mystruct
MyStruct(m1=1, m2=2)

Structure instances follow the same behavior list allowing access to the members by the normal list indexing:

>>> mystruct[0] == 1
True
>>> mystruct[1] == 2
True

In addition, structure types support accessing members as an attribute:

>>> mystruct.m1 == 1
True
>>> getattr(mystruct, 'm2') == 2
True