问题描述
我有枚举并使用诸如 myEnum.SomeNameA
、myEnum.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:
- http://groups.google.com/group/comp.lang.python/msg/237dc92f3629dd9a?pli=1
- http://aspn.activestate.com/ASPN/Mail/消息/python-Tutor/330294
也许您可以使用一种解决方法将值转换回名称/表示字符串.如果您发布更多示例代码和有关您想要此内容的详细信息,也许我们可以提供更深入的帮助.
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 中打印原始变量的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!