V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Claude Code 订阅服务平替
更经济的 Claude Code 订阅平替服务低至10元/天,同等于 Claude Max 200美元方案,支持Claude 4 Opus模型
Promoted by ylsAGI
coolair
V2EX  ›  问与答

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

  •  
  •   coolair · 2021-03-09 14:59:52 +08:00 · 559 次点击
    这是一个创建于 1589 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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   ·   实用小工具   ·   942 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 20:34 · PVG 04:34 · LAX 13:34 · JFK 16:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.