Please view the struct doc document first
struct.pack (fmt, v1, v2, ...)
Returns a string containing the values ββv1, v2, ... packed in accordance with this format. The arguments must exactly match the values ββneeded for the format.
-
struct.unpack (fmt, string)
Unpack the string (supposedly packaged pack (fmt, ...)) in accordance with this format. The result is a tuple even if it contains exactly one element. The string must contain exactly the amount of data required by the format (len (string) must equal calcsize (FMT)).
Because struct.pack defined as struct.pack(fmt, v1, v2, ...) . It takes a list of arguments without a keyword ( v1, v2, ... , aka *args ), so struct.unpack needs to return the list as an object, therefore a tuple.
It would be easy to see if you consider the package as
x = struct.pack(fmt, *args) args = struct.unpack(fmt, x) # return *args
Example:
>>> x = struct.pack(">i", 1) >>> struct.unpack(">i", x) (1,) >>> x = struct.pack(">iii", 1, 2, 3) >>> struct.unpack(">iii", x) (1, 2, 3)