Python: builtins部分方法takes no keyword arguments原因

Quotation:
Due to the way the Python C-level APIs developed, a lot of built-in functions and methods don’t actually have names for their arguments. Even if the documentation calls the argument default, the function doesn’t recognize the name default as referring to the optional second argument. You have to provide the argument positionally:

>> d = {1: 2}
>> d.get(0, default=0)
Traceback (most recent call last):
File “”, line 1, in
TypeError: get() takes no keyword arguments
>> d.get(0, 0)
0

–You hinted, but didn’t say explicitly: python functions usually allow arbitrary named arguments. The C-level API is an exception to the rule. – idbrii Aug 7 '15 at 17:22

–Thanks, although it’s more annoying if I want to debug current code – Dejell Feb 14 '17 at 8:43
2
–Wow I used python for 7 years, and I never knew there were functions that don’t take keyword arguments. That’s so strange, surely the interpreter could maintain a dictionary of builtin functions and their argument names, so that it could accept keyword arguments with nearly zero runtime cost? (The only cost would be during the generation of bytecode, and it would be very small.) – max Jun 19 '17 at 1:19

@max: Parameter names can’t be resolved at compile time, because what callable you’re calling can’t be resolved at compile time. Making it a compile-time thing wouldn’t intrinsically make all C functions and methods take keyword arguments, anyway; either way, people would have to go through and add all the missing parameter name metadata by hand, and they could (but don’t) do that under the current design. – user2357112 Jun 19 '17 at 1:38
This is the real answer. – abalcerek Oct 4 '17 at 19:20

(quote from user2357112, Stack Overflow)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值