coolair
V2EX  ›  问与答

问下如下代码 Python 类为啥有不同的结果?

  •  
  •   coolair · Mar 9, 2021 · 842 views
    This topic created in 1913 days ago, the information mentioned may be changed or developed.
    class Test(models.Model):
        name = models.CharField('名称', max_length=64, unique=True)
    
        def __init__(self, *args, **kwargs):
            super(Test, self).__init__(*args, **kwargs)
            print(self, type(self))    # 为啥这里 self 是空白?
    
    
    class Test(object):
        def __init__(self, name):
            print(self)  # 为啥这里 self 有东西?
            self.name= name
    
    test = Test(name='aaa')    # 输出<Test object at 0x000002A3E4016F70>
    
    coolair
        1
    coolair  
    OP
       Mar 9, 2021
    我的锅,哈哈。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3170 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:58 · PVG 21:58 · LAX 06:58 · JFK 09:58
    ♥ Do have faith in what you're doing.