Discuss / Python / 觉得廖老师要讲一下dict的继承

觉得廖老师要讲一下dict的继承

Topic source

看ORM的一些提示

1. User类在Modelmetaclass函数中attrs的初始状态用print打印为

{'__module__': '__main__', '__qualname__': 'User', 'id': <__main__.IntegerField object at 0x0000020A18381A60>, 'name': <__main__.StringField object at 0x0000020A18381AC0>, 'email': <__main__.StringField object at 0x0000020A18381B20>, 'password': <__main__.StringField object at 0x0000020A18381B80>} 

改变后的状态为

{'__module__': '__main__', '__qualname__': 'User', '__mappings__': {'id': <__main__.IntegerField object at 0x000002228EFB1A60>, 'name': <__main__.StringField object at 0x000002228EFB1AC0>, 'email': <__main__.StringField object at 0x000002228EFB1B20>, 'password': <__main__.StringField object at 0x000002228EFB1B80>}, '__table__': 'User'}

故在save()函数中,__mappings__的值为

{'id': <__main__.IntegerField object at 0x000001FAD5EA1A60>, 'name': <__main__.StringField object at 0x000001FAD5EA1AC0>, 'email': <__main__.StringField object at 0x000001FAD5EA1B20>, 'password': <__main__.StringField object at 0x000001FAD5EA1B80>}

u = User(id=12345, name='Michael', email='test@orm.org', password='my-pwd')

u所传入的值,仅仅在

def __getattr__(self, key):  
        try:
            
            return self[key]
        except KeyError:
            raise AttributeError(r"'Model' object has no attribute '%s'" % key)


args.append(getattr(self, k, None))

中使用,其他地方均没有使用

由于继承dict,要用u['id']调用。

Senior198303

#2 Created at ... [Delete] [Delete and Lock User]

由于继承dict,要用u['id']调用。

Senior198303

#3 Created at ... [Delete] [Delete and Lock User]

由于继承dict,要用u['id']调用。

---------------------------------------------

不是继承dict的关系,而是**kw传进去就是一个dict


  • 1

Reply