python version of javascript#39;s arguments object - does it exist?(javascript 参数对象的 python 版本 - 它存在吗?)
问题描述
在 JavaScript 中,每个函数都有一个特殊的 arguments
预定义对象,其中包含有关传递给函数调用的参数的信息,例如
函数测试() {var args = Array.prototype.slice.call(arguments);控制台.log(args);}
参数可以轻松转储到标准数组:
test()//[]测试(1,2,3)//[1, 2, 3]测试(你好",123,{},[],函数(){})//["hello", 123, Object, Array[0], function]
我知道在 Python 中我可以使用标准参数、位置参数和关键字参数(就像定义的 here) 来管理动态参数编号 - 但是有没有类似于 Python 中的 arguments
对象的东西?
它在 python 中并不存在,但你可以调用 locals() 作为函数中的第一件事,此时它应该只有参数
p>
>>>定义 f(a,b):... args = locals()... 对于 arg,args.items() 中的值:...打印参数,值... 返回 a*b...
In JavaScript each function has a special arguments
predefined objects which holds information about arguments passed to the function call, e.g.
function test() {
var args = Array.prototype.slice.call(arguments);
console.log(args);
}
arguments can be easily dumped to a standard array:
test()
// []
test(1,2,3)
// [1, 2, 3]
test("hello", 123, {}, [], function(){})
// ["hello", 123, Object, Array[0], function]
I know that in Python I can use standard arguments, positional arguments and keyword arguments (just like defined here) to manage dynamic parameter number - but is there anything similar to arguments
object in Python?
It doesnt exist as is in python but you can call locals() as the first thing in the function and at that point it should only have the arguments
>>> def f(a,b):
... args = locals()
... for arg, value in args.items():
... print arg, value
... return a*b
...
这篇关于javascript 参数对象的 python 版本 - 它存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:javascript 参数对象的 python 版本 - 它存在吗?


基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01