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
3
votes
3 answers

How do I unpack a map into keyword arguments for string format in Golang?

In Python, to format a string with a dictionary, one can simply do: geopoint = { 'latitude': 41.123, 'longitude':71.091 } print('{latitude} {longitude}'.format(**geopoint)) The output will be 41.123 71.091. How do I achieve the same keyword…
Jason Kao
  • 1,024
  • 11
  • 15
3
votes
1 answer

How can I annotate f(*params)?

I cannot work out how to correctly annotate this code: from typing import Iterable def f(*params: Iterable) -> str: return ":".join(params) I know that Iterable is incorrect, because mypy tells me: error: Argument 1 to "join" of "str" has…
3
votes
1 answer

Pass tuple to function that takes *args and **kwargs

I have two functions: def foo(*args, **kwargs): pass def foo2(): return list(), dict() I want to be able to pass the list and dict from foo2 as args and kwargs in foo, however when I use it like foo(foo2()) or foo(*foo2()) tuple returned…
Bob K.
  • 33
  • 3
3
votes
2 answers

Variadic template: error: parameter packs not expanded with '...'

I'm trying to pass multiple strings to fill a Container, but I receive this error. Using gcc 4.9.3 template< class T > struct DataCompare { bool operator()( const T& lhs, const T& rhs ) const { return operator<( lhs->getCode(),…
rh0x
  • 1,066
  • 1
  • 13
  • 33
3
votes
3 answers

Use a tuple as arguments for a function in Elixir

I'm writing a game engine in Elixir. (Yes, I know it's not a language inherently suited to that – the point is to look at how the use of an atypical language affects the structure of the result.) As such, I have several supervisors to be run on game…
Vivian
  • 1,539
  • 14
  • 38
2
votes
1 answer

How can I unpack variadic template, so as to initialize respective members?

I am new to variadic templates and packed arguments and all. I want to have a "entity component system" in my program, and while trying to add component to the entity, I came to realize that my attempt has a major flaw. Anyways here is my failed…
2
votes
1 answer

Cannot use positional arguments after argument unpacking while preparing a statement

I am trying to bind a variable number of values into the IN () condition of my prepared statement AND bind a few more values later in the query, but I am getting an error: PHP Fatal error: Cannot use positional argument after argument…
2
votes
1 answer

Python 3.4 Syntax error and how to fix it

While the following line is accepted on Python 3.6, On Python 3.4 I am getting a syntax error: struct.pack_into('q' * len(GeoFence_lat_list)*2,buff,264,*GeoFence_lat_list, *GeoFence_lon_list) Where GeoFence_lon_list is an array declared…
jpvans
  • 59
  • 6
2
votes
1 answer

Is it possible to unpack a generic tuple via pattern matching in Rust?

It is possible to perform the following: fn foo((a, b): (i32, f32)) { unimplemented!(); } and it can be executed via: let ab = (1, 2.0); foo(ab); In effect ab is unpacked into a and b separately. I'd like to do something similar with…
Saxpy
  • 129
  • 1
  • 7
2
votes
1 answer

Conditional Argument Expansion in Function Call

I want to conditionally replace a single line in a file, depending on some pre-conditions and write the new content. The way i choose was: n_file = [] For line in file.getlines(): n_file.append( line.replace( *("foo", "bar") if…
2
votes
1 answer

What does the asterisk do in *a, b, c = line.split()?

Assume line is: "Chicago Sun 01:52". What does *a, b, c = line.split() do? In particular, what is the significance of the asterisk? Edit: Upon testing it, it seems like "Chicago", "Sun" and "01:52" are all stored in a, b and c. The asterisk seems…
Sahand
  • 7,980
  • 23
  • 69
  • 137
2
votes
2 answers

Use single construct for multiple arguments

In an effort to avoid magic numbers and a bit of future-proofing, i'd like to be able to declare a single constant or variable with multiple constituent elements, to enable a single point to change the values in future. For…
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
2
votes
1 answer

Is there a better (i.e. more Pythonic) way to deal with packing/unpacking function arguments?

I'm writing a method to "adds ticks" to an object. (What "adding ticks" means doesn't matter for my question. You can also ignore the 'batch' variable in my code samples.) All the ticks will have the same width and length, but are at different tick…
JonathanZ
  • 1,046
  • 11
  • 20
2
votes
1 answer

unpacking arguments to a function if variable not empty

I'm trying to pass in a list of arguments to function if the variable is not empty. Here's the variable: shape_properties: { 'header_shape': { 'width': 8582400, …
Boosted_d16
  • 13,340
  • 35
  • 98
  • 158
2
votes
1 answer

Function argument unpacking error

Look at this example (using python 2.7.6): >>> def func(a, b, c, d): print a, b, c, d >>> func(1, c = 3, *(2,), **{'d':4}) 1 2 3 4 Up to here, this is fine. But, why the following call fails? >>> func(1, b = 3, *(2,), **{'d':4}) Traceback…
user3022222
  • 857
  • 9
  • 9