python中怎样在定义一个对象的时候取得对象的名称
大家讲道理
大家讲道理 2017-04-18 09:19:24
0
1
394

怎样在定义一个对象的时候取得对象的名称,例如:

class TheThing(): 
    def __init__(self): 
        self.name = str(self) #此处想获得赋值对象的名称
        print(self.name)

a = TheThing()

此时想要得到a.name==’a’,该如何实现

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
刘奇

It cannot be implemented because TheThing() will be executed first to obtain an instance of TheThing, and then a will be pointed to this instance. a is just a yin and yang. When executing "a=", TheThing's __init__ has already been executed

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template