为什么 Python 仅允许命名的参数在函数调用中跟随 tuple 拆解表达式?
>>> def f(a,b,c):
... print a, b, c
...
>>> f(*(1,2),3)
File "<stdin>", line 1
SyntaxError: only named arguments may follow *expression
这仅仅是一种审美选择,还是存在允许这样做会导致一些含糊不清的情况?
为什么 Python 仅允许命名的参数在函数调用中跟随 tuple 拆解表达式?
>>> def f(a,b,c):
... print a, b, c
...
>>> f(*(1,2),3)
File "<stdin>", line 1
SyntaxError: only named arguments may follow *expression
这仅仅是一种审美选择,还是存在允许这样做会导致一些含糊不清的情况?
我敢肯定,人们"自然"不喜欢这个原因 是因为它使后期争论的意义模糊不清 取决于内推系列的长度
def dangerbaby(a, b, *c):
hug(a)
kill(b)
>>> dangerbaby( puppy , bug )
killed bug
>>> cuddles = [ puppy ]
>>> dangerbaby(*cuddles, bug )
killed bug
>>> cuddles.append( kitten )
>>> dangerbaby(*cuddles, bug )
killed kitten
仅看最后两通呼叫dangerbaby
无法分辨出其中一通是否如预期那样有效,一通是杀死小猫的飞毛腿。
当然,当最后的内插时,也存在这种不确定性的某些因素。但混乱受内插序列的限制,它不影响其他论点,例如bug
。
[我快速搜索了一下看能否找到任何官方信息。 似乎上一个语法是 < http://www.python.org/search/hypermail/python- 1992/0260.html> rel='research/hypermail/python- 1992/ 0260.html> rel='noreferrer'> 讨论在这里 , 以及它如何运作的规则相当复杂。 因为在最后添加了额外的“必须”的论据, 而最后似乎没有这样的标记。 最后有一个
我怀疑它是为了与函数定义中的恒星符号保持一致,而函数定义则是在函数调用中的恒星符号模型之后。
在下一个定义中,参数c
将模糊所有随后的非关键词参数,因此,当调用 f
时,显然只有将d
的值传递给 d
的唯一方法才能作为关键词参数。
def f(a, b, *c, d=1):
print "slurped", len(c)
(Python 3. 在 Python 2 中, is 无法在恒星参数之后指定值,因此上述参数是非法的。 )
所以,在函数定义 中,恒星参数必须遵循所有普通位置参数。 您观察到的是, 相同的规则已经延伸至函数调用。 这样, 恒星语法对于函数声明和函数调用是一致的 。
另一个平行的是,您只能在函数调用中有一个( single-) 恒星参数。 以下是违法的, 尽管人们可以很容易地想象它被允许 。
f(*(1,2), *(3,4))
首先,使用一个折叠式函数提供非常相似的界面很简单:
def applylast(func, arglist, *literalargs):
return func(*(literalargs + arglist))
applylast(f, (1, 2), 3) # equivalent to f(3, 1, 2)
其次,提高翻译支持您的本地语法可能会增加功能应用中非常关键的功能性活动的管理费。 即使由于这些常规的使用率很高,只要在编译代码中增加一些额外的指示,这也可能构成一种令人无法接受的性能处罚,作为交换条件,因为用户图书馆通常不方便地提供所有这些功能。
一些意见:
f(c=3, *(1, 2))
in your example still prints 1 2 3
). This makes sense as (i) most arguments in function calls are positional and (ii) the semantics of a programming language need to be unambiguous (i.e., a choice needs to be made either way on the order in which to process positional and keyword arguments).f(*(1, 2), 3)
, should that be f(1, 2, 3)
or f(3, 1, 2)
and why would either choice make more sense than the other?def g(a, b, *c, d)
. There s no way to provide a value for d
other than as a keyword argument (positional arguments would be grabbed by c
).更改顺序:
def f(c,a,b):
print(a,b,c)
f(3,*(1,2))
如果您有一个 Python 3 仅关键字参数, 例如
def f(*a, b=1):
...
然后,你可能会期望 f(*,(1,2),3,
设置
>
>
>>(1,2)
>和 b
>
>>
3>(3,2,3,3)
,但当然,即使允许使用您想要的语法,它也不会使用,因为只有关键词参数必须是关键词,例如 f(*,1,2,b=3),
。如果允许,我想它必须设置
,并留 a
,2,3,
作为默认的
b
,1。因此,它可能不会使用语法上的模糊性模糊性,因为期望中的模糊性太过模糊性太多,这是Pythson 想要避免。
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...