argument-unpacking

Difference call function with asterisk parameter and without

核能气质少年 提交于 2019-12-03 16:49:50
问题 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 is the difference between the first and the second function call? 回答1: Let args = [1,2,3] : func(*args) == func(1,2,3) - variables are unpacked out of list (or any other sequence type) as parameters func(args) == func([1,2,3]) - the list is passed Let kwargs

Go Unpacking Array As Arguments

ε祈祈猫儿з 提交于 2019-12-03 02:49:38
问题 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 well! Something along the lines of this: func my_func(a, b int) (int) { return a + b } func main() { arr := []int{2,4} sum := my_func(arr) } I do apologize if I'm making an syntactical/assorted mistakes. I'm new to Go. 回答1: You can use a vararg

Go Unpacking Array As Arguments

浪子不回头ぞ 提交于 2019-12-02 15:00:44
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 well! Something along the lines of this: func my_func(a, b int) (int) { return a + b } func main() { arr := []int{2,4} sum := my_func(arr) } I do apologize if I'm making an syntactical/assorted mistakes. I'm new to Go. You can use a vararg syntax similar to C: package main import "fmt" func my_func( args ...int) int { sum := 0 for _,v := range args

Unpack nested list for arguments to map()

半世苍凉 提交于 2019-12-01 17:54:22
问题 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 the first iteration, i.e., it does add(1, 2) , then add(3, 4) for the second, etc.? Trying map(add, foo) obviously does add([1, 2], #nothing) for the first iteration Trying map(add, *foo) does add(1, 3, 5) for the first iteration I want something like map(add, foo)

Where are python's splat operators * and ** valid?

北慕城南 提交于 2019-11-30 07:07:38
问题 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 ---------------------------------------------------------------------- function(*args) ✓ ✓ ✓ x, *y, z = [1, 2, 3, 4, 5] x ✓ ✓ {**x, **y} x x ✓ Are there any more discrepancies between the various versions that I've missed? I'm looking through PEP and Readmes but the docs aren't detailed with this. 回答1: Around 1992 (not sure

Passing an Array/List into Python

牧云@^-^@ 提交于 2019-11-28 15:58:54
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 work as I want. I'm used to be able to pass arrays into PHP function with ease and this is confusing me. It also seems I can't do this: def someFunc(*args, someString) As it throws up an error. I think I've just got myself completely confused and looking for someone to clear it up for me. When you define your function using this syntax: def someFunc(*args) for x

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

十年热恋 提交于 2019-11-28 00:39:29
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 "<stdin>", line 1 [1,2,3, *[4,5,6]] ^ SyntaxError: invalid syntax Why doesn't the * operator: [1, 2, 3, *[4, 5, 6]] give [1, 2, 3, 4, 5, 6] whereas when the * operator is used with a function call it does expand: f(*[4, 5, 6]) is equivalent to f(4, 5, 6) There is a similarity between the + and the * when using lists but not when extending a list

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

匆匆过客 提交于 2019-11-28 00:11:59
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 I do A(inds) I actually get d separate values from A , which is not what I want. What I want is for element i of inds to be the ith parameter in the function call A (). One solution is to create a comma-separated list out of your vector of subscripted indices inds . You can do this by converting it to a cell array using NUM2CELL , then using the {:

Passing an Array/List into Python

蓝咒 提交于 2019-11-27 09:25:06
问题 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 work as I want. I'm used to be able to pass arrays into PHP function with ease and this is confusing me. It also seems I can't do this: def someFunc(*args, someString) As it throws up an error. I think I've just got myself completely confused and looking for

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

眉间皱痕 提交于 2019-11-27 03:55:50
This question already has an answer here: Use of *args and **kwargs [duplicate] 11 answers 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)) You can unpack a tuple or a list into positional arguments using a star. def add(a, b, c): print(a, b, c) x = (1, 2, 3) add(*x) Similarly, you can use double star to unpack a dict into keyword arguments. x = { 'a': 3, 'b': 1, 'c': 2 } add(**x) I think you mean the * unpacking operator : >>> l