do not subclass built-in classes in python

In short, they will not behave what you think they will. you think dict.get in implemented in python, like:

def get(self, key, default=None):
    try:
        return self[key]
    except KeyError:
        return default

However, they dict.get do not call getitem, and they are implemented in c separately, and do not call each other.

so, when you implement geitem, get is not automatically altered! which is very dangerous.

What you need do, subclass collections.abc.Mapping

source:

[1] http://www.kr41.net/2016/03-23-dontinheritpythonbuiltindict_type.html

及时获取更新,请关注公众号“爬虫技术学习(spider-learn)”

公众号“爬虫技术学习(spider-learn)”

About 逸飞

后端工程师

发表评论

电子邮件地址不会被公开。 必填项已用*标注