JavaScript的参数对象的python版本 - 它存在吗? [英] python version of javascript's arguments object - does it exist?

查看:113
本文介绍了JavaScript的参数对象的python版本 - 它存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,每个函数都有一个特殊的参数预定义对象,它保存有关传递给函数调用的参数的信息,例如

  function test(){
var args = Array.prototype.slice.call(arguments);
console.log(args);
}

参数可以轻松转储到标准数组中:

  test()
// []

test(1,2,3)
/ / [1,2,3]

test(hello,123,{},[],function(){})
// [hello,123,Object ,数组[0],函数]

我知道在Python中我可以使用标准参数,和关键字参数(就像定义的这里)来管理动态参数号 - 但是在Python中有没有类似于 arguments 对象的东西?

解决方案

它不像python中的那样存在,但是你可以调用locals()作为函数中的第一件事,并且在那一点上它应该只有参数

 >>> def f(a,b):
... args = locals()
... for arg,args.items()中的值:
... print arg,value
...返回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版本 - 它存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆