V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
coolair
V2EX  ›  问与答

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

  •  
  •   coolair · 2021-03-09 14:59:52 +08:00 · 424 次点击
    这是一个创建于 1214 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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
       2021-03-09 15:15:19 +08:00
    我的锅,哈哈。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4316 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:55 · PVG 17:55 · LAX 02:55 · JFK 05:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.