我有一个我需要调试的代码错误,但我不确定我哪里出错了。当我运行我的代码时,我得到了:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
c3_plus_0i = create_complex(create_ordinary(3), create_ordinary(0))
File "<pyshell#30>", line 2, in create_complex
return get("make","complex")(x,y)
TypeError: 'NoneType' object is not callable
这是我的代码:
from generic_arith_min import *
def install_complex_package():
def make_com(x,y):
return tag(repcom(x,y))
def repcom(x,y):
return (x,y)
def real(x):
return x[0]
def imag(x):
return x[1]
def tag(x):
return attach_tag("complex",x)
# add,sub,mul,div: (RepCom, RepCom) -> Generic-Com
def add_com(x,y):
return make_com( add(real(x), real(y)),
add(imag(x), imag(y)) )
def sub_com(x,y):
return make_com( sub(real(x), real(y)),
sub(imag(x), imag(y)) )
def mul_com(x,y):
return make_com(sub(mul(real(x), real(y)),
mul(imag(x), imag(y))),
add(mul(real(x), imag(y)),
mul(real(y), imag(x))))
def div_com(x,y):
com_conj = complex_conjugate(y)
x_times_com_conj = content(mul_com(x, com_conj))
y_times_com_conj = content(mul_com(y, com_conj))
return make_com(div(real(x_times_com_conj), real(y_times_com_conj)),
div(imag(x_times_com_conj), real(y_times_com_conj)));
def complex_conjugate(x):
return (real(x),negate(imag(x)))
def negate_com(x): # (RepCom) -> Generic-Com
return make_com(negate(real(x)), negate(imag(x)))
def is_zero_com(x): # (RepCom) -> Py-Bool
if is_zero(real(x)) and is_zero(imag(x)):
return True
else:
return False
def is_eq_com(x,y): # (RepCom, RepCom) -> Py-Bool
if is_equal(real(x),real(y)) and is_equal(imag(x),imag(y)):
return True
else:
return False
put("make","complex", make_com)
put("add",("complex","complex"), add_com)
put("sub",("complex","complex"), sub_com)
put("mul",("complex","complex"), mul_com)
put("div",("complex","complex"), div_com)
put("negate",("complex",), negate_com)
put("is_zero",("complex",), is_zero_com)
put("is_equal",("complex","complex"), is_eq_com)
def repord_to_repcom(x):
return repcom(create_ordinary(x),create_ordinary(0))
def CCmethod_to_OCmethod(method):
return lambda ord, com: method(repord_to_repcom(ord), com)
def CCmethod_to_COmethod(method):
return lambda com, ord: method(com, repord_to_repcom(ord))
put("add",("ordinary","complex"), CCmethod_to_OCmethod(add_com))
put("add",("complex","ordinary"), CCmethod_to_COmethod(add_com))
put("sub",("ordinary","complex"), CCmethod_to_OCmethod(sub_com))
put("sub",("complex","ordinary"), CCmethod_to_COmethod(sub_com))
put("mul",("ordinary","complex"), CCmethod_to_OCmethod(mul_com))
put("mul",("complex","ordinary"), CCmethod_to_COmethod(mul_com))
put("div",("ordinary","complex"), CCmethod_to_OCmethod(div_com))
put("div",("complex","ordinary"), CCmethod_to_COmethod(div_com))
put("is_equal",("ordinary","complex"), CCmethod_to_OCmethod(is_eq_com))
put("is_equal",("complex","ordinary"), CCmethod_to_COmethod(is_eq_com))
def create_complex(x,y):
return get("make","complex")(x,y)
#################
# Do not change #
#################
n3 = create_ordinary(3)
c3_plus_0i = create_complex(create_ordinary(3), create_ordinary(0))
c2_plus_7i = create_complex(create_ordinary(2), create_ordinary(7))
def gradeThis_complex_package():
complexA = is_equal(n3, c3_plus_0i)
complexB = is_equal(sub(add(n3, c2_plus_7i), c2_plus_7i), n3)
if complexA and complexB:
print("Well done! Your install_complex_package is complete!")
else:
print("Please check your solution for install_complex_package.")
gradeThis_complex_package()
以下是从中导入的受支持文件:
_operation_table = {} #variables with prefixed with an _ are by convention, for internal use and should not be touched.
def attach_tag(tag, content):
return (tag, content)
def type_tag(datum):
if type(datum) == tuple and len(datum) == 2:
return datum[0]
raise Exception('Bad tagged datum -- type_tag ', datum)
def content(datum):
if type(datum) == tuple and len(datum) == 2:
return datum[1]
raise Exception('Bad tagged datum -- content ', datum)
def put(op, types, value):
if op not in _operation_table:
_operation_table[op] = {}
_operation_table[op][types] = value
def get(op, types):
if op in _operation_table and types in _operation_table[op]:
return _operation_table[op][types]
else:
return None
def apply_generic(op, *args):
type_tags = tuple(map(type_tag, args))
proc = get(op, type_tags)
if proc:
return proc(*map(content, args))
raise Exception('No method for these types -- apply_generic', (op, type_tags))
##########################
# Generic Number Package #
##########################
#generic operators we want to support
def add(x,y):
return apply_generic("add", x, y)
def sub(x,y):
return apply_generic("sub", x, y)
def mul(x,y):
return apply_generic("mul", x, y)
def div(x,y):
return apply_generic("div", x, y)
def negate(x):
return apply_generic("negate", x)
def is_zero(x):
return apply_generic("is_zero", x)
def is_equal(x, y):
return apply_generic("is_equal", x, y)
#composite generic operators
def square(x):
return mul(x,x)
#Generic ordinary number package
def install_ordinary_package():
def make_ord(x):
return tag(x)
def tag(x):
return attach_tag("ordinary", x)
# add,sub,mul,div: (RepOrd, RepOrd) -> Generic-OrdNum
def add_ord(x,y):
return make_ord(x+y)
def sub_ord(x,y):
return make_ord(x-y)
def mul_ord(x,y):
return make_ord(x*y)
def div_ord(x,y):
return make_ord(x/y)
def negate_ord(x): # (RepOrd) -> Generic-Ord
return make_ord(-x)
def is_zero_ord(x): # (RepOrd) -> Py-Bool
return x == 0
def is_equal_ord(x,y): # (RepOrd, RepOrd) -> Py-Bool
return x == y
put("make","ordinary", make_ord)
put("negate",("ordinary",), negate_ord)
put("is_zero",("ordinary",), is_zero_ord)
put("add",("ordinary","ordinary"), add_ord)
put("sub",("ordinary","ordinary"), sub_ord)
put("mul",("ordinary","ordinary"), mul_ord)
put("div",("ordinary","ordinary"), div_ord)
put("is_equal",("ordinary","ordinary"), is_equal_ord)
install_ordinary_package()
def create_ordinary(x):
return get("make", "ordinary")(x)
上述代码究竟在哪里导致出现此错误?如果有人可以提供帮助,我会非常感激。谢谢!
答案 0 :(得分:1)
您的_operation_table
字典不包含make
和complex
的条目,因此您的get()
函数返回None
,实际上它不可调用。
print("complex" in _operation_table["make"])
False
也许您忘记拨打install_complex_package
(与调用install_ordinary_package
的方式相同),将所需的功能添加到_operation_table
。一旦调用,您的代码就可以正常工作。