keyword-argument

What is the purpose of bare asterix in function arguments?

﹥>﹥吖頭↗ 提交于 2019-11-29 13:23:26
I've seen this SO question (this is not a duplicate): Python bare asterisk in function argument In python-3.x you can add a bare * to the function arguments, this means that (quote from docs ): Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments. Ok, so, I've defined a function: >>> def f(a, b, *, c=1, d=2, e=3): ... print('Hello, world!') ... I can pass c , d and e variable values only by specifying keywords: >>> f(1, 2, 10, 20, 30) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes 2

Keyword arguments unpacking (splat) in Ruby

白昼怎懂夜的黑 提交于 2019-11-29 09:59:01
What is happening below seems a little strange to me. def f(a, b) puts "#{a} :: #{b}" end f(*[1, 2], **{}) # prints "1 :: 2" hash = {} f(*[1, 2], **hash) ArgumentError: wrong number of arguments (3 for 2) f(*[1, 2], **Hash.new) ArgumentError: wrong number of arguments (3 for 2) Is this a compiler optimization feature? That is a Ruby's bug that has been reported several times (for example here by me) but has not been fixed. I guess that since the keyword argument feature has been introduced, the double splat syntax has become murky, and that is the indirect cause of this bug. I heard that Matz

understanding '*' “keyword only” argument notation in python3 functions [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 07:15:29
This question already has an answer here: Bare asterisk in function arguments? 4 answers I am having some difficulty behaviour of keyword only arguments feature in python3 when used with partial . Other info on keyword only arguments. Here is my code: def awesome_function(a = 0, b = 0, *, prefix): print('a ->', a) print('b ->', b) print('prefix ->', prefix) return prefix + str(a+b) Here is my understanding of partial: >>> two_pow = partial(pow, 2) >>> two_pow(5) 32 >>> What I understood is in the above example, partial makes the second argument to pow function as the only argument of two_pow .

Is is safe to use a function accepts kwargs keyword arguments that are not identifiers?

梦想的初衷 提交于 2019-11-28 04:05:51
问题 In Python, is it safe to give keyword arguments that are not Python identifiers to a function? Here is an example: >>> '{x-y}'.format(**{'x-y': 3}) # The keyword argument is *not* a valid Python identifier '3' >>> '{x-y}'.format(x-y=3) File "<ipython-input-12-722afdf7cfa3>", line 1 SyntaxError: keyword can't be an expression I am asking this because it is more convenient for me to format with names that contain a dash (because the values correspond to command-line argument with dashes in

Keyword arguments unpacking (splat) in Ruby

限于喜欢 提交于 2019-11-28 02:54:04
问题 What is happening below seems a little strange to me. def f(a, b) puts "#{a} :: #{b}" end f(*[1, 2], **{}) # prints "1 :: 2" hash = {} f(*[1, 2], **hash) ArgumentError: wrong number of arguments (3 for 2) f(*[1, 2], **Hash.new) ArgumentError: wrong number of arguments (3 for 2) Is this a compiler optimization feature? 回答1: That is a Ruby's bug that has been reported several times (for example here by me) but has not been fixed. I guess that since the keyword argument feature has been

Most pythonic way of assigning keyword arguments using a variable as keyword?

只愿长相守 提交于 2019-11-27 09:05:44
What is the most pythonic way to get around the following problem? From the interactive shell: >>> def f(a=False): ... if a: ... return 'a was True' ... return 'a was False' ... >>> f(a=True) 'a was True' >>> kw = 'a' >>> val = True >>> f(kw=val) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() got an unexpected keyword argument 'kw' For the moment I'm getting around it with the following: >>> exec 'result = f(%s=val)' % kw >>> result 'a was True' but it seems quite clumsy... (Either python 2.7+ or 3.2+ solutions are ok) Use keyword argument unpacking : >>>

Most pythonic way of assigning keyword arguments using a variable as keyword?

余生长醉 提交于 2019-11-26 14:32:32
问题 What is the most pythonic way to get around the following problem? From the interactive shell: >>> def f(a=False): ... if a: ... return 'a was True' ... return 'a was False' ... >>> f(a=True) 'a was True' >>> kw = 'a' >>> val = True >>> f(kw=val) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() got an unexpected keyword argument 'kw' For the moment I'm getting around it with the following: >>> exec 'result = f(%s=val)' % kw >>> result 'a was True' but it