How to get values from a class that has __slots__ and _fields_?

Viewed 42

I am using an API library that has the following implementaion of C code to the position and rotation in a Python program:

class struct_LinmathPose(Structure):
    pass

struct_LinmathPose.__slots__ = [
    'Pos',
    'Rot',
]
struct_LinmathPose._fields_ = [
    ('Pos', LinmathPoint3d),
    ('Rot', LinmathQuat),
]

And one of the variables is of this class. How can I get and access for example the position of this variable? LinmathPoint3d and LinmathQuat are C codes.

1 Answers

__slots__ (which doesn't actually work as you wrote it; it's making an unused class attribute, not actually defining slots) and _fields_ just declare variables by string name. For _fields_ said variables are default initialized if they're not passed to the constructor; for __slots__ they're empty until assigned. Either way, accessing them is like any other variable; you have an instance of the class, and you do .Pos or the like:

pose = struct_LinmathPose()
pose.Pos  # Reads the attribute
Related