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
3315
votes
27 answers

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

What do *args and **kwargs mean in these function definitions? def foo(x, y, *args): pass def bar(x, y, **kwargs): pass See What do ** (double star/asterisk) and * (star/asterisk) mean in a function call? for the complementary question…
Todd
  • 34,500
  • 4
  • 23
  • 13
786
votes
4 answers

What do ** (double star/asterisk) and * (star/asterisk) mean in a function call?

In code like zip(*x) or f(**k), what do the * and ** respectively mean? How does Python implement that behaviour, and what are the performance implications? See also: Expanding tuples into arguments. Please use that one to close questions where OP…
176
votes
4 answers

Go Unpacking Array As Arguments

So in Python and Ruby there is the splat operator (*) for unpacking an array as arguments. In Javascript there is the .apply() function. Is there a way of unpacking an array/slice as function arguments in Go? Any resources for this would be great as…
eatonphil
  • 13,115
  • 27
  • 76
  • 133
134
votes
4 answers

Unpacking, extended unpacking and nested extended unpacking

Consider the following expressions. Note that some expressions are repeated to present the "context". (this is a long list) a, b = 1, 2 # simple sequence assignment a, b = ['green', 'blue'] # list asqignment a, b…
treecoder
  • 43,129
  • 22
  • 67
  • 91
76
votes
4 answers

Class that acts as mapping for **unpacking

Without subclassing dict, what would a class need to be considered a mapping so that it can be passed to a method with **. from abc import ABCMeta class uobj: __metaclass__ = ABCMeta uobj.register(dict) def f(**k): return k o =…
dskinner
  • 10,527
  • 3
  • 34
  • 43
67
votes
3 answers

How to extract parameters from a list and pass them to a function call

What is a good, brief way to extract items from a list and pass them as parameters to a function call, such as in the example below? Example: def add(a,b,c,d,e): print(a,b,c,d,e) x=(1,2,3,4,5) add(magic_function(x))
falek.marcin
  • 9,679
  • 9
  • 28
  • 33
66
votes
6 answers

Passing an array/list into a Python function

I've been looking at passing arrays, or lists, as Python tends to call them, into a function. I read something about using *args, such as: def someFunc(*args) for x in args print x But not sure if this is right/wrong. Nothing seems to…
Jack Franklin
  • 3,765
  • 6
  • 26
  • 34
60
votes
3 answers

Cannot unpack array with string keys

The error message is: FATAL ERROR Uncaught Error: Cannot unpack array with string keys I know I can simply run the method fetch() twice and pass the ['q'] and ['bind'], but I am trying to get to grips with using the new ... to unpack values. I…
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
55
votes
2 answers

Python 3: starred expression to unpack a list

Example use: def f(a, b, c, d): print(a, b, c, d, sep = '&') f(1,2,3,4) >>> 1&2&3&4 f(*[1, 2, 3, 4]) >>> 1&2&3&4 Where in the python documentation is * explained?
Kifsif
  • 3,477
  • 10
  • 36
  • 45
32
votes
3 answers

Python: Splat/unpack operator * in python cannot be used in an expression?

Does anybody know the reasoning as to why the unary (*) operator cannot be used in an expression involving iterators/lists/tuples? Why is it only limited to function unpacking? or am I wrong in thinking that? For example: >>> [1,2,3, *[4,5,6]] File…
Har
  • 3,727
  • 10
  • 41
  • 75
31
votes
3 answers

Unpack NumPy array by column

If I have a NumPy array, for example 5x3, is there a way to unpack it column by column all at once to pass to a function rather than like this: my_func(arr[:, 0], arr[:, 1], arr[:, 2])? Kind of like *args for list unpacking but by column.
jeff_new
  • 389
  • 2
  • 4
  • 12
31
votes
3 answers

Why this unpacking of arguments does not work?

I get an error type object argument after ** must be a mapping, not tuple. I have this code: create_character = player.Create(**generate_player.generate()) this is player.py module: class Create(object): def __init__(self,name,age,gender): …
user3056783
  • 2,265
  • 1
  • 29
  • 55
23
votes
5 answers

Python keyword arguments unpack and return dictionary

I have a function definition as below and I am passing keyword arguments. How do I get to return a dictionary with the same name as the keyword arguments? Manually I can do: def generate_student_dict(first_name=None, last_name=None , birthday=None,…
Karan Kumar
  • 2,655
  • 19
  • 36
18
votes
4 answers

python - iterating list of dictionaries and unpacking

Given a flat list of simple dictionaries lst = [{'key1': 1}, {'key2': 2}, {'key3': 3}] I'd like to find the dict that yields the minimum value evaluated using a method not detailed here. My first idea was to iterate the list to check dict by dict,…
jake77
  • 1,892
  • 2
  • 15
  • 22
18
votes
1 answer

How are python's unpacking operators * and ** used?

The unpacking/splat operators * and ** differ widely in their applicability across python versions (2.7, 3.x < 3.5 and 3.x >= 3.5). For example: | 2.7 | 3.1-3.4 | 3.5 …
cs95
  • 379,657
  • 97
  • 704
  • 746
1
2 3
9 10