What does "gather" mean in computing?

Viewed 89

I'm learning Python.

In "Python For Everyone" by Charles Severance, in the Glossary section for Tuples I came across the following definition:

Gather: The operation of assembling a variable-length argument tuple.

Right now this is Greek for me, and the vocable isn't mentioned anywhere else in the book I'm learning from. Not even with examples.

I would like to know what does it mean.

1 Answers

They probably mean iterable packing or unpacking with *, but I wouldn't get hung up on the terminology

Simple unpacking

>>> t1 = (1,2,3)
>>> t2 = (4,5,6)
>>> (*t1, *t2)              # unpack two tuples
(1, 2, 3, 4, 5, 6)
>>> "{}:{}".format(*(1,2))  # unpacking a tuple
'1:2'
>>> "{}:{}".format(*[1,2])  # unpacking a list
'1:2'
>>> "{b}:{a}".format(**{'a':1, 'b':2})  # unpacking a dict
'2:1'

More complete example with function arguments
(from @Tomerikoo's comment, specifically getting multiple args with *args (as 'c' and 'd' are below) may be what the glossary means by gathering)

>>> def foo(arg1, arg2, *args, **kwargs):
...     print(f"{arg1}|{arg2}|{args}|{kwargs}")
...
>>> foo(*('a', 'b', 'c', 'd'), foo="bar")
a|b|('c', 'd')|{'foo': 'bar'}

a and b are unpacked into arg1 and arg2
.. while b and c are packed into args
foo="bar" gives a keyword argument, which is packed into the kwargs dict (as it's not explicitly specified before it)


Additionally
If someone discussed gathering with me in a Python context, I'd assume they meant asyncio.gather(), which is not necessarily gathering simple tuples, but a collection of outputs which may not be immediately available, though the premise is comparable (and .gather() itself takes a variable number of arguments which are packed into its *aws, awaitables arg)

Related