蟒蛇:如何获得参数的名称运行 [英] Python: How to get argument's name runtime

查看:120
本文介绍了蟒蛇:如何获得参数的名称运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Python :可能是如此明显,:)反正,我们正在寻找 ,如下:<? / p>

in Python: may be so obvious, :) anyway, we are looking for ?, below:

def printname(x):
   print ?,x

>>> a = 1.3
>>> printname(a)
>>> 'a',1.3

这样的东西,而不是重新present传递参数的名称。结果
如果不是很明显的任何伎俩或解决方案?

so something instead of ? to represent the name of passed argument.
if not that obvious ? any trick or solution?

推荐答案

这不是的无法的。您需要遍历备份堆栈帧,并找到code调用该函数的行。

It's not impossible. You need to traverse back up stack frames and find the line of code that calls the function.

这是一个好的黑客用于调试的事,但没有办法,我会在现实code

It's an ok hack to do for debugging, but no way I would use it in real code

下面是给你一个起点:

import inspect
def printname(x):
    print inspect.stack()[1][4]

a = 1.3
printname(a)

这篇关于蟒蛇:如何获得参数的名称运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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