Apr-07-2025, 04:31 PM
Hi, I am curios whether it is possible to have a class define specific members based on a "type". I want to implement something like this:
I could think of something with the
enum type { REQUEST_A = 1, REQUEST_B = 2 }; struct packet { enum type type; union { struct a_args_s { int arg1; } a_args; struct b_args_s { int arg1; int arg2; } b_args; }; };The
enum type
can be done with enum.IntEnum
but I am not sure how to do the struct packet
. I know it could be done with ctypes
but I want to do with with pure Python.I could think of something with the
__setattr__()
method, where only the correct attributes are allowed to be set but I was wondering if there was a better way?