在Python中调用方法的当前类版本

时间:2019-05-10 13:22:39

标签: python python-3.x inheritance

我有两类继承层次结构:

_treePane = new JTreePane();
DefaultTableColumnModel model = new DefaultTableColumnModel();

for (int i = 0; i < (Cols.length - 2); i++) 
    {
            if (i == 0) 
            {
                ColumnWidth[i] = ((Double)(width * 0.17)).intValue();  //This is equal to 180
                a = new TableColumn(i, ColumnWidth[i], null, null);

                ColumnWidth[i] = ((Double)(width * 0.1846)).intValue(); //This is equal to 195
            }
            else if(i==1 || i ==2)
            {
                ColumnWidth[i] = ((Double)(width * 0.1231)).intValue();  //This is equal to 130
                a = new TableColumn(i, ColumnWidth[i], null, null);
            }
            else if(i == 3)
            {
                ColumnWidth[i] = ((Double)(width * 0.1136)).intValue(); //This is equal to 120
                                a = new TableColumn(i, ColumnWidth[i], null, null);
            }
            else if(i == 4)
            {
                ColumnWidth[i] = ((Double)(width * 0.1294)).intValue(); //This is equal to 105 0.0994
                a = new TableColumn(i, ColumnWidth[i], null, null);
            }
            else if( i == 5)
            {
                ColumnWidth[i] = ((Double)(width * 0.09575)).intValue();  //This is equal to 80
                a = new TableColumn(i, ColumnWidth[i], null, null);
            }
            else if(i == 7)
            {
                ColumnWidth[i] = ((Double)(width * 0.08575)).intValue();
                a = new TableColumn(i, ColumnWidth[i], null, null);
            }
            else if(i==8)
            {
                ColumnWidth[i] = ((Double)(width * 0.01575)).intValue();
                a = new TableColumn(i, ColumnWidth[i], null, null);
            }
            else if(i== 6)
            {
                ColumnWidth[i] = ((Double)(width * 0.1183)).intValue(); //This is equal to 125
                a = new TableColumn(i, ColumnWidth[i], null, null);
            }
            else
                a = new TableColumn(i, ColumnWidth[i], null, null);


      a.setHeaderValue(temp);
      a.setHeaderRenderer(headRend);
      model.addColumn(a);
    }

JTable tempTable = new JTable();
tempTable.setColumnModel(model);
tempTable.setName("TempTable");
_treeHeader = tempTable.getTableHeader();
_treeHeader.setResizingAllowed(true);

_treePane.setColumnHeaderView(_treeHeader);

但是,这会导致无限递归错误,因为每当我尝试获取class TfIdfSimilarity(SimilarityModel): @property def corpus(self): return self.model[self._corpus] @property def model(self): return TfidfModel(self._corpus, dictionary=self._dict) class LsiSimilarity(TfIdfSimilarity): @property def corpus(self): tfidf_corpus = super().corpus return self.model[tfidf_corpus] @property def model(self): tfidf_corpus = super().corpus return LsiModel(tfidf_corpus) 时,它都会调用LsiSimilarity#model,然后它会尝试访问模型,从而调用TfIdfSimilarity#corpus再次。

我真正想要的是让LsiSimilarity#model仅调用TfIdfSimilarity#corpus,而不考虑子类的版本。

这在Python中可行吗? (Python 3)如果没有,我该如何更好地构造继承层次结构,同时仍然通过TfIdfSimilarity#modelcorpus函数(由我的主要逻辑使用)提供相同的API。

2 个答案:

答案 0 :(得分:2)

您可以invoke descriptors directly,方法是从类中获取对描述符的引用,然后调用描述符的__get__方法:

TfIdfSimilarity.model.__get__(self)

如果您发现自己经常这样做,则可能表明您应该使用常规方法而不是属性。

答案 1 :(得分:1)

您始终可以指定要使用的方法,而不用self.method_xy来调用SpecificClass.method(self, other_arguments)

我首先错过了属性装饰器的含义,而是通过调用__get__TfIdfSimilarity.model.__get__(self))切换到解决方案,但是由于这是对私有方法的调用,因此我不喜欢该解决方案。

受此How to call a property of the base class if this property is being overwritten in the derived class?的启发,我找到了方法fget

最小示例

class Test:
    def __init__(self, text):
        self._text = text

    @property
    def text(self):
        return self._text

class InheritTest(Test):

    @property
    def text(self):
        return "test" + self._text    


test = InheritTest("Test")
print(Test.text.fget(test))
# Test
print(test.text)
# testTest