TypeError:object .__ new __()在使用继承时不带参数

时间:2011-03-01 15:16:29

标签: python inheritance typeerror

嘿 我很难实现某些东西,我想这应该不会很难。我一直在阅读很多帖子,我仍然无法弄明白,虽然它可能已经回答了,我可能只是不明白答案:/

所以,我有一个类,在逻辑中定义一个算法文件three_dpll.py /以及一些辅助函数

class three_dpll(object):
    ...
    def __extend__(self, symbol, value, mod):
            """ __extend__(symbol, value) - extends the model
            ...
            """

    def three_dpll(self, formula, symbols, mod):
            """ three_dpll(formula, symbols, mod) - calculates 3-DPLL \n
            NOTE: This algorithm should not be overwritten in any derived class!!"""
            ...
            # find unit clause
            curr_data = self.__find_unit_clause__(formula, mod)
            current_symbol = curr_data[0]
            current_symbol_set.add(current_symbol)
            current_value = curr_data[1]
            if current_symbol != None: 
                return three_dpll(formula, symbols - current_symbol_set,
                                  self.__extend__(current_symbol, current_value, mod))
            ...

以及应该为某个逻辑实现算法的逻辑,我可能会重新定义某些方法,例如来自logics.three_dpll.py(或任何其他帮助函数)

from three_dpll import three_dpll
class kleene_logic(three_dpll):
    """ This is the definition of Kleene logic """
    pass

现在从另一个文件中的函数调用它:

def satisfiable(logic, formula):
    """ satisfiable - \
    takes a logic and a set of formula and returns true or false"""
    # model is empty dictionary
    model = {}
    # symbols is a set
    symbols = set()
    my_logic = "logics."+logic # logic is passed as string to the script
    __import__(my_logic, fromlist=['three_dpll'])
    log = modules[my_logic]
    used_logic = log.kleene_logic()
    for clause in formula:
        ite = iter(clause)
        for literal in ite:
            symbols.add(abs(literal))
    try:
        return used_logic.three_dpll(formula, symbols, model)
    except FormulaValid.FormulaValid:
        return True

我得到的错误是:

in three_dpll
    self.__extend__(current_symbol, current_value, mod)) TypeError: object.__new__() takes no parameters

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:3)

你的班级three_dpll也有一个方法three_dpll。当你

return three_dpll(...)

您正在创建该类的实例(而不是调用该方法,这可能是您想要做的)。该类没有__init__()函数可以处理您提供的参数。这就是错误消息告诉您的内容。

你想要的可能是

return self.three_dpll(...)

会调用该方法。如果它能解决你的问题,那就不是一般的,但这应该解释一下。