类型错误:使用继承时object.__new__()不接受参数
你好,
我在实现一个东西时遇到了困难,我觉得这应该不算太难。我看了很多帖子,但还是搞不明白,可能这个问题已经有人回答过,只是我没理解答案 :/
我有一个类,定义了一个算法文件叫做 three_dpll.py,放在 logics/ 文件夹里,还有一些辅助函数。
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 个回答
3
你的类叫做 three_dpll,但它里面也有一个方法也叫 three_dpll。当你
return three_dpll(...)
在创建这个类的实例时(而不是调用那个方法,这可能是你真正想做的)。这个类里没有 __init__()
这个函数来处理你传入的参数。这就是错误信息想告诉你的内容。
你可能想要的是类似下面这样的
return self.three_dpll(...)
这样可以调用那个方法。虽然我不确定这是否能解决你的问题,但这应该能解释一些事情。