Skip to content
Advertisement

What does the comma mean in Python’s unpack?

We can simply use:

crc = struct.unpack('>i', data)

why do people write it like this:

(crc,) = struct.unpack('>i', data)

What does the comma mean?

Advertisement

Answer

The first variant returns a single-element tuple:

In [13]: crc = struct.unpack('>i', '0000')

In [14]: crc
Out[14]: (808464432,)

To get to the value, you have to write crc[0].

The second variant unpacks the tuple, enabling you to write crc instead of crc[0]:

In [15]: (crc,) = struct.unpack('>i', '0000')

In [16]: crc
Out[16]: 808464432
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement