我有一個簡單的類,我創建了兩個對象。我現在想要在類中打印對象的名稱。因此,像這樣:如何從類內部獲取對象名稱?
class Example:
def printSelf(self):
print self
object1 = Example()
object2 = Example()
object1.printSelf()
object2.printSelf()
我需要這個打印:
object1
object2
可惜這只是打印<myModule.Example instance at 0xb67e77cc>
有誰知道我能做到這一點?
通過對象的名稱,你的意思是 - 引用的名稱(object1,object2,...)?? –
是的。不好意思,我會就此編輯我的問題。 – kramer65