如何获取Python对象类的变量名 [英] How to get the variable name of a Python object class
本文介绍了如何获取Python对象类的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道这是否可能/如何可能。
I would like to know if this is possible/how it is possible.
我有一个Python对象类:
I have a Python object class:
class myClass(object):
def __init__(self):
...do something
然后我创建该对象的实例:
Then I create an instance of that object:
myObjectClass = myClass()
来自 myClass
,是否可以将变量( myObjectClass
)的名称作为字符串获取?
From within myClass
, is it possible to get the "name" of the variable (myObjectClass
) as a string?
谢谢,
亚当
推荐答案
除某些骇人听闻的方式外,这绝对不可能。
It's definitely not possible, other than some hackish way.
为什么不只是在初始化中添加 name
参数,像这样:
Why not just add a name
parameter to init, like this:
def __init__(self, name):
self.name = name
myObjectClass = myClass("myObjectClass")
这就是我的实际操作方式。
This is how I'd actually do it..
这篇关于如何获取Python对象类的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文