search

Home  >  Q&A  >  body text

python中怎样在定义一个对象的时候取得对象的名称

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

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

a = TheThing()

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

大家讲道理大家讲道理2893 days ago395

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:21:24

    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

    reply
    0
  • Cancelreply