簡體   English   中英

TypeError:“ NoneType”對象不可調用(Python)

[英]TypeError: 'NoneType' object is not callable (Python)

我有一個代碼錯誤需要調試,但是我不確定哪里出錯了。 當我運行代碼時,我得到:

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)

上面的代碼到底在什么地方引起了此錯誤? 如果有人可以提供幫助,我將非常感激。 謝謝!

您的_operation_table詞典不包含帶有makecomplex的條目,因此您的get()函數返回None ,這實際上是不可調用的。

 print("complex" in _operation_table["make"])
 False

也許您忘記了調用install_complex_package (以與您調用install_ordinary_package相同的方式),該函數將所需的函數添加到_operation_table 一旦調用,您的代碼就可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM