Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack / pack operator

I'm looking to find various ways that a packing / unpacking operator is implemented. As an example:

*[1,2,3]    --> 1,2,3     (one array scalar value unpacked to three values)
*1,2,3      --> [1,2,3]   (three values packed to one scalar array value)

Is this a common operator in languages? If so, how is it usually represented?

Other possible terms:

  • Destructuring / structuring
  • Unpacking / packing
like image 424
David542 Avatar asked Aug 31 '26 15:08

David542


2 Answers

The way to achieve this depends on the language.

In python values can be packed / unpacked using tuples:

mytuple = (1,2,3)   # Packing into a tuple
(a,b,c) = mytuple   # Unpacking
print(f'{a}, {b}, {c}')

In c++11, it can be done by using std::tie():

#include <iostream>
#include <tuple>

int main()
{
    auto mytuple = std::tuple<int,int,int>{1,2,3}; // Packing into a tuple
    int a, b, c;
    std::tie(a,b,c) = mytuple;                     // Unpacking (std::tie)
    std::cout << a << " " << b << " " << c << std::endl;
    return 0;
}

And, in c++17 and newer, structured bindings can be used:

#include <iostream>
#include <tuple>

int main()
{
    auto mytuple = std::tuple{1,2,3}; // Packing into a tuple
    auto[a,b,c] = mytuple;            // Unpacking (structured binding)
    std::cout << a << " " << b << " " << c << std::endl;
    return 0;
}
like image 132
Patricio Loncomilla Avatar answered Sep 02 '26 07:09

Patricio Loncomilla


I will describe the whole related works and syntax in Python.

Packing variables as a collection and unpacking iterables to some variables:

iterable = (1, 2, 3, 4)  # Packing by Tuple explicitly
iterable = 1, 2, 3, 4  # Packing by Tuple implicitly
iterable = [1, 2, 3, 4]  # Packing by List
nested_iterable = (1, 2, (4, 5, 6))  # Packing nested items
mapping = {"a": 1, "b": 2}

a, b, c, d = iterable  # Unpacking using Tuple implicitly
(a, b, c, d) = iterable  # Unpacking using Tuple explicitly
[a, b, c, d] = iterable  # Unpacking using List
a, _, _, d = iterable  # Ignoring some values by conventionally using the _ (called Throwaway) variable
a, *middle_items, d = iterable  # Using * to group remaining items as a List
a, b, c, *d = iterable  # Using * for a single element is also grouped as a single item List
a, b, c, d, *e = iterable  # If there are no other elements, e becomes an empty List
a, *_ = iterable  # Ignoring remaining items
a, b, (c, d, e) = nested_iterable  # Unpacking nested items using Tuple
a, b, [c, d, e] = nested_iterable  # Unpacking nested items using mixed Tuple and List
a_key, b_key = mapping  # The items get iterated for unpacking, so, here, only keys got returned
(a_key, a_value), (b_key, b_value) = mapping.items()  # Unpacking a mapping using items method

Note: You cannot apply the * operator multiple times in a single unpacking.

Unpacking iterables as positional arguments and mappings as named arguments along with Packing remaining positional and named arguments:

def normal_function(a, b, c, d):
    ...

items = [2, 3, 4, 5]
mapping = {"a": 2, "b": 3, "c": 4, "d": 5}

normal_function(*items)  # Positional unpacking to function arguments.
normal_function(**mapping)  # Named unpacking to function arguments.

def variadic_function(a, b, c, *remaining_positional_args, d, e, f, **remaining_named_args):
    # remaining_positional_args is a Tuple and remaining_named_args is a Dict
    # d, e, f must be filled by the name
    ...
like image 43
Alireza Roshanzamir Avatar answered Sep 02 '26 07:09

Alireza Roshanzamir