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.

119 questions
2675
votes
23 answers

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

In the following method definitions, what does the * and ** do for param2? def foo(param1, *param2): def bar(param1, **param2):
Todd
  • 28,050
  • 4
  • 20
  • 13
660
votes
5 answers

What does the star and doublestar operator mean in a function call?

What does the * operator mean in Python, such as in code like zip(*x) or f(**k)? How is it handled internally in the interpreter? Does it affect performance at all? Is it fast or slow? When is it useful and when is it not? Should it be used in a…
122
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
  • 10,877
  • 21
  • 66
  • 118
113
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
  • 36,160
  • 18
  • 57
  • 89
67
votes
2 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
  • 9,259
  • 1
  • 27
  • 41
56
votes
5 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,603
  • 6
  • 24
  • 34
52
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
  • 7,006
  • 8
  • 25
  • 32
41
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
  • 2,793
  • 10
  • 30
  • 36
27
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,005
  • 8
  • 27
  • 63
26
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
  • 289
  • 1
  • 4
  • 12
22
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,636
  • 17
  • 36
20
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
  • 1,224
  • 1
  • 15
  • 36
17
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
  • 274,032
  • 76
  • 480
  • 537
14
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,364
  • 2
  • 11
  • 19
14
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
  • 958
  • 9
  • 17
1
2 3 4 5 6 7 8