方法说明
1、__or__和__ror__魔法方法对应|操作符___or__表示对象在操作符的左侧,__ror__表示对象在操作符的右侧。新字典是根据左侧的操作量生成的,右侧的操作量更新到新字典,然后返回新字典。
2、__ior__魔法方法对应|=操作符,右侧操作数可自行更新。
实例
def__or__(self,other): ifnotisinstance(other,dict): returnNotImplemented new=dict(self) new.update(other) returnnew def__ror__(self,other): ifnotisinstance(other,dict): returnNotImplemented new=dict(other) new.update(self) returnnew def__ior__(self,other): dict.update(self,other) returnself
以上是python dict实现的魔法方法,希望对大家有所帮助。学习更多的编程基础知识:python学习网
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。