TypeError:多个基地有实例布局冲突 [英] TypeError: multiple bases have instance lay-out conflict

查看:46
本文介绍了TypeError:多个基地有实例布局冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下两个类中创建一个类: collections.OrderedDict collections.DefaultDict .这样我就可以获得有序的字典,并为正在访问的不存在的键提供默认值.有什么方法可以做到这一点?

I wanted to create a class out of two: collections.OrderedDict and collections.DefaultDict. So that I can get an ordered dictionary and have a default value for non existing keys being accessed. Whats are some ways to do this?

我的解决方案是围绕上述两个类创建另一个类.由于每个类中的方法具有我认为的相同名称,这会导致错误?

My solution was to create another class around the 2 classes I stated above. This makes an error due to a method in each class having the same name I think?

from collections import defaultdict, OrderedDict
class owndic(OrderedDict, defaultdict):
    pass

生产

TypeError: multiple bases have instance lay-out conflict

干杯!

推荐答案

实例布局冲突是一种奇特的说法,表示您试图从多个继承(在这种情况下是内置的)-in--无法很好地相互配合的类型.从技术上讲,这些类的通用属性在功能上存在冲突.

Instance lay-out conflict is a fancy way of saying that you're trying to inherit from multiple--in this case built-in--types that cannot cooperate with each other quite well. More technically these classes have conflict in functionality of their common attributes.

在这种情况下, OrderedDict defaultdict 都是两个类似字典的类型,具有各自独特的 __ setitem __ 属性,并且使用不同的方式处理键和值.

In this case both OrderedDict and defaultdict are two dictionary-like types with their own unique __setitem__ attributes and also different ways of handling the keys and values.

在C级别,当 best_base 函数无法计算多个基类中的最佳基数.还有许多其他原因可能导致此函数无法计算其他基准中的最佳基准或获胜者,但是在这种情况下,当以下两个条件均未发生时,将发生错误. winner 不是None, winner 不是候选 * 候选的子类型不是 winner 的子类型.

At C level this error happens when the best_base function fails to calculate the best base amongst multiple base classes. There are multiple other reasons that can cause this function fail to calculate the best base or winner among other bases but in this case the error occurs when neither of following conditions happen. winner is not None, winner is not a subtype of candidate*, candidate is not a subtype of winner.

candidate = solid_base(base_i);
if (winner == NULL) {
    winner = candidate;
    base = base_i;
}
else if (PyType_IsSubtype(winner, candidate))
    ;
else if (PyType_IsSubtype(candidate, winner)) {
    winner = candidate;
    base = base_i;
}
else {
    PyErr_SetString(
        PyExc_TypeError,
        "multiple bases have "
        "instance lay-out conflict");
    return NULL;

}

但是,如果您想同时使用 defaultdict() OrderedDict()的功能,则只需使用 OrderedDict 及其内置的 setdefault 属性.

However, if you want to benefit from both defaultdict() and OrderedDict()'s functionalities, you can simply use an OrderedDict and its built-in setdefault attribute.

这里是一个例子:

In [13]: d = OrderedDict()

In [14]: for i, j in enumerate(['k', 'r', 'k', 'j', 'j', 't']):
             d.setdefault(j, []).append(i)
   ....:     

In [15]: d
Out[15]: OrderedDict([('k', [0, 2]), ('r', [1]), ('j', [3, 4]), ('t', [5])])

这篇关于TypeError:多个基地有实例布局冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆