从函数返回后,如何在 Python 中打印原始变量的名称? [英] How to print original variable's name in Python after it was returned from a function?

查看:68
本文介绍了从函数返回后,如何在 Python 中打印原始变量的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有枚举并使用诸如 myEnum.SomeNameAmyEnum.SomeNameB 等变量.当我从函数返回这些变量之一时,我可以打印它们吗?名称(例如 myEnum.SomeNameA)而不是它们返回的值?

I have enum and use the variables like myEnum.SomeNameA, myEnum.SomeNameB, etc. When I return one of these variables from a function, can I print their names (such as myEnum.SomeNameA) instead of the value they returned?

推荐答案

简短回答:没有.

长答案:使用回溯、检查等进行一些丑陋的黑客攻击是可能的,但通常可能不建议将其用于生产代码.例如见:

Long answer: this is possible with some ugly hacks using traceback, inspect and the like, but it's generally probably not recommended for production code. For example see:

也许您可以使用一种解决方法将值转换回名称/表示字符串.如果您发布更多示例代码和有关您想要此内容的详细信息,也许我们可以提供更深入的帮助.

Perhaps you can use a workaround to translate the value back to a name/representational string. If you post some more sample code and details about what you're wanting this for maybe we can provide more in-depth assistance.

这篇关于从函数返回后,如何在 Python 中打印原始变量的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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