"Stephen Nelson-Smith" <sanel...@gmail.com> wrote

To upack your variables a and b you need an iterable object on the right
side, which returns you exactly 2 variables

What does 'unpack' mean?  I've seen a few Python errors about packing
and unpacking.  What does it mean?

It has a coup[le of uses, the one being referred to here is where we
take a sequence of values and assign each value to a corresponding
variable on the other side of the assignment.

x,y = (val1, val2)

which is equivalent to

x = val1; y = val2

or

j,k = [3,4]

j=3; k=4

You can also pack a set of variables into a single value:

t = 3,4,5

or

s = 4,5,6

But this is really just tuple assignment without the parentheses! :-)

Pack/Unpack can also refer to an operation of the struct module,
which does a similar thing with binary data.

So if we have a set of bytes b we can extract the bytes into a
set of variables using a format string and the struct.unpack()
operation. Conversely you use struct.pack to encode a sequence
of values into a sequence of bytes.

But thats a more specialised usage, not seen so often.

HTH

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to