Python 3.x 如何打印位置参数
我有这个示例代码和nee要打印出来,例如,仅第二个参数的位置,我该怎么做Python 3.x 如何打印位置参数,python-3.x,arguments,Python 3.x,Arguments,我有这个示例代码和nee要打印出来,例如,仅第二个参数的位置,我该怎么做 def fun(a, b, c): d = locals() e = d print (e) print (locals()) fun(None, 2, None) 打印(b)。。。或者我不明白这个问题 更新:如果您想知道参数的名称,可能需要使用名为inspect的标准模块。请尝试以下操作: #!python3 import inspect def fun(a, b, c):
def fun(a, b, c):
d = locals()
e = d
print (e)
print (locals())
fun(None, 2, None)
打印(b)
。。。或者我不明白这个问题
更新:如果您想知道参数的名称,可能需要使用名为inspect
的标准模块。请尝试以下操作:
#!python3
import inspect
def fun(a, b, c):
d = locals()
e = d
print (e)
print (locals())
# Here for observing from inside.
argspec = inspect.getfullargspec(fun)
print(argspec.args)
for arg in argspec.args:
print('argument', repr(arg), '=', repr(d[arg]))
# You can use indexing of the arg names if you like. Then the name
# is used for looking in locals() -- here you have it in d.
args = argspec.args
print(d[args[0]])
print(d[args[1]])
print(d[args[2]])
fun(None, 2, None)
# Here for observing from outside.
argspec = inspect.getfullargspec(fun)
print(argspec.args)
for n, arg in enumerate(argspec.args, 1):
print('argument', n, 'is named', repr(arg))
你应注意:
{'a': None, 'b': 2, 'c': None}
{'d': {...}, 'e': {...}, 'a': None, 'b': 2, 'c': None}
['a', 'b', 'c']
argument 'a' = None
argument 'b' = 2
argument 'c' = None
None
2
None
['a', 'b', 'c']
argument 1 is named 'a'
argument 2 is named 'b'
argument 3 is named 'c'
参见文档谢谢,我需要一个代码,如:for arguments in fun[1](位置2等)。我想要的是一种方式,以所有的立场,而不是辩论我的编辑按钮不工作的一些未知的原因。我想编辑这个Q来阅读;调用参数位置,如fun[1](位置2等)。您可以调用字符串位置的方式