V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
coolair
V2EX  ›  问与答

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

  •  
  •   coolair · Mar 9, 2021 · 759 views
    This topic created in 1876 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   ·   1007 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:18 · PVG 03:18 · LAX 12:18 · JFK 15:18
    ♥ Do have faith in what you're doing.