Questions tagged [argument-unpacking]

Use this tag for questions related with argument unpacking, a technique that allows Arrays and Traversable objects to be extracted/unpacked into argument lists/sequences.

is usually found in PHP, Python and Go.

144 questions
16
votes
3 answers

Difference call function with asterisk parameter and without

I know what the meaning of an asterisk is in a function definition in Python. I often, though, see asterisks for calls to functions with parameters like: def foo(*args, **kwargs): first_func(args, kwargs) second_func(*args, **kwargs) What…
Robert Moon
  • 1,025
  • 10
  • 17
9
votes
1 answer

Auto-unpacking a pair of iterators

In C++, if a function returns a std::pair, we can auto-receive it as follows: auto pr = some_function(); std::cout << pr.first << ' ' << pr.second; Now, C++17 standard provides a beautiful way of unpacking this pair directly into separate…
MrProgrammer
  • 443
  • 3
  • 13
8
votes
1 answer

Python - value unpacking order in method parameters

def fun(a, b, c, d): print('a:', a, 'b:', b, 'c:', c, 'd:', d) why this one works fun(3, 7, d=10, *(23,)) and prints out: a: 3 b: 7 c: 23 d: 10 while this fun(3, 7, c=10, *(23,)) does not Traceback (most recent call last): File…
Łukasz
  • 1,980
  • 6
  • 32
  • 52
8
votes
1 answer

How to overload * argument unpacking operator?

I have data like data = [[t1, t2, ...], [v1, v2, ...]]. I want to wrap this in a class so I can call data.t instead of having to use data[0]. I tried to do this with the following: class Variable: def __init__(self, data): self.t =…
pingul
  • 3,351
  • 3
  • 25
  • 43
7
votes
2 answers

Packing values into a tuple using *, just like function argument packing

Consider a function defined as: def fun(a, *args): print(type(args), args) When called, it packs the extra positional arguments as a tuple. >>> fun(2, 3, 4) (3, 4) I want to achieve a similar thing outside of function…
2020
  • 2,821
  • 2
  • 23
  • 40
6
votes
2 answers

MATLAB: Accessing an element of a multidimensional array with a list

I have a d-dimensional array, A, and vector inds with length equal to d. I would like to access the value of A at inds. Ideally, I'd do something like A(*inds) (borrowing the unpacking syntax from Python). I'm not sure how to do this in MATLAB. If…
emchristiansen
  • 3,550
  • 3
  • 26
  • 40
6
votes
3 answers

How to ignore unpacked parts of a tuple as argument of a lambda?

In Python, by convention, the underscore (_) is often used to throw away parts of an unpacked tuple, like so >>> tup = (1,2,3) >>> meaningfulVariableName,_,_ = tup >>> meaningfulVariableName 1 I'm trying to do the same for a tuple argument of a…
Julian
  • 4,170
  • 4
  • 20
  • 27
6
votes
1 answer

Unpacking a dictionary and passing to a function as keyword parameters

I am trying in python to unpack some dict into some function: I have a function that get packet as parameter (that should be dict) def queue(self, packet): self.topic.publish(self.message, self.client, **packet) and I call it this way: queue({ …
ET-CS
  • 6,334
  • 5
  • 43
  • 73
5
votes
4 answers

Syntax error when passing unpacked argument to print in Python

Instead of a simple debug/log print as this: print "error ", error_number I would like to use a log function that I can expand when required looking something like this: def log(condition, *message): if(): …
hobb
  • 166
  • 1
  • 10
5
votes
3 answers

Unpack variadic template to initializer_list and call two functions at once

I have two vectors: std::vector v1{ 1, 2, 3 }; std::vector v2{ 4, 5, 6 }; I want to create an object of std::initializer_list which holds iterators to the first and last elements of above vectors. I want to have a function with a variadic…
5
votes
3 answers

Unpack nested list for arguments to map()

I'm sure there's a way of doing this, but I haven't been able to find it. Say I have: foo = [ [1, 2], [3, 4], [5, 6] ] def add(num1, num2): return num1 + num2 Then how can I use map(add, foo) such that it passes num1=1, num2=2 for…
binaryfunt
  • 6,401
  • 5
  • 37
  • 59
5
votes
2 answers

Unpacking Parameter Pack in C++

I have two functions f and g. f calculates it's return value asynchronously and returns a future. Now, based on several return values of f, I want to call g, but I want to make sure that the computations of the values of f happen in…
ssb
  • 7,422
  • 10
  • 36
  • 61
4
votes
2 answers

Understanding the asterisk operator in python when it's before the function in a parenthesis

I know that the asterisk is used to unpack values like system args or when you unpack lists into variables. But I have not seen this syntax here before in this example of asyncio. I was reading this article here,…
anarchy
  • 3,709
  • 2
  • 16
  • 48
4
votes
6 answers

Why does max(*list) and max(list) do the same thing in python?

See below example: >>>f = [[1],[2],[3]] >>>max(f) Out[21]: [3] >>>max(*f) Out[22]: [3] The unpack operator did not have an effect here, I am trying to unpack a list and get a maximal value of matrix(two dim list).
Pythoner
  • 5,265
  • 5
  • 33
  • 49
4
votes
2 answers

How does ruby unpack arguments passed into Proc?

a_proc = Proc.new {|a,b,*c| p c; c.collect {|i| i*b }} puts a_proc[2,2,4,3] Code above is pretty intuitive according to https://ruby-doc.org/core-2.2.0/Proc.html, a_proc[2,2,4,3] is just a syntax sugar for a_proc.call(2,2,4,3) to hide “call” But…
Ze Gao
  • 91
  • 1
  • 7
1
2
3
9 10