【问题标题】:TypeError: object.__new__() takes no parameters when using inheritanceTypeError: object.__new__() 使用继承时不带参数
【发布时间】:2011-07-06 14:21:34
【问题描述】:

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

所以,我有一个类,在 logics/ 中定义了一个算法文件 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

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 你不应该像使用__extend__ 那样编造__magic__ 方法,它们被编译器非常特殊地对待。我想你只是想要_private(带有一个前导下划线)方法。

标签: python inheritance typeerror


【解决方案1】:

您的类three_dpll 也有一个方法three_dpll。当你

return three_dpll(...)

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

你想要的可能是这样的

return self.three_dpll(...)

这将调用该方法。不确定它是否能解决您的问题,但这应该可以解释一些事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2016-04-03
    • 2013-06-09
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多