我有这个示例代码和nee打印输出,例如单独的第二个论点位置,我该怎么做?
def fun(a, b, c):
d = locals()
e = d
print (e)
print (locals())
fun(None, 2, None)
答案 0 :(得分:0)
print(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'
请参阅文档http://docs.python.org/3.3/library/inspect.html#inspect.getfullargspec