Python类和函数的非类型错误

Python类和函数的非类型错误,python,class,nonetype,object-oriented-analysis,Python,Class,Nonetype,Object Oriented Analysis,尽管我的函数返回了一个数组,我还是得到了一个TypeError:“NoneType”对象不可调用错误。我首先定义类,然后插入函数。 我对Python比较陌生,如果您能提供帮助/意见,我将不胜感激 课程包括: from scipy import array, dot, zeros, empty from scipy.optimize import brentq from numpy import * from numpy.random import uniform from time impor

尽管我的函数返回了一个数组,我还是得到了一个
TypeError:“NoneType”对象不可调用
错误。我首先定义类,然后插入函数。
我对Python比较陌生,如果您能提供帮助/意见,我将不胜感激

课程包括:

from scipy import array, dot, zeros, empty
from scipy.optimize import brentq
from numpy import *
from numpy.random import uniform 
from time import time

# from mpi4py import MPI

class UtilTheory:
    """

    """

    def utility(self, other):
        raise NotImplementedError("You need to implement method 'utility' "
                                  "in the child class.")

    def demand(self, p):
        raise NotImplementedError("You need to implement method 'demand' "
                                  "in the child class.")


    def excessDemand(self, p):
        p = array(p)
        return self.demand(p) - self.endowment

    def indUtility(self, p):
        x = self.demand(p)
        return self.utility(x)

    def __add__(self, other):
        economy = Economy([self, other])

        return economy

    def __radd__(self, other):
        economy = Economy([self] + other)

    def __rmul__(self, other):
        economy = Economy(other * [self])

        return economy

class Consumer(UtilTheory, object):
    """
    Defines general features of a consumer
    """

    def __init__(self, endowment=array([1,1]), alpha=0.5, rho=0.0):
        self.endowment = array(endowment)
        self.alpha = float(alpha)
        self.rho = rho
        self.sigma = 1.0 / (1.0 - rho)

    def __str__(self):
        return ('e=' + str(self.endowment) +
                ', alpha=' + str(self.alpha) +
                ', rho=' + str(self.rho))

    def demand(self, p):
        endowment = self.endowment
        alpha = self.alpha
        sigma = self.sigma
        p = array(p)
        m = dot(endowment, p)

        x1 = ((alpha / p[0]) ** sigma * m /
              (alpha ** sigma * p[0] ** (1 - sigma) +
              (1 - alpha) ** sigma * p[1] ** (1 - sigma)))

        x2 = (((1.0 - alpha) / p[1]) ** sigma * m /
              (alpha ** sigma * p[0] ** (1 - sigma) +
              (1 - alpha) ** sigma * p[1] ** (1 - sigma)))

        return array([x1, x2])

    def utility(self, x):
        if self.rho != 0:
            return ((self.alpha * x[0] ** self.rho +
                    (1.0 - self.alpha) * x[1] ** self.rho) **
                       (1.0 / self.rho))

        return x[0] ** self.alpha * x[1] ** (1.0 - self.alpha)

class Economy:
    """
    Consists of consumers
    """

    def __init__(self, consumers):
        """
        Consumers should be a list of consumers
        """

        self.consumers = consumers

    def excessDemand(self, p):
        result = zeros(2)
        for consumer in self.consumers:
            result = result + consumer.excessDemand(p)

        return result

    def demand(self, p):
        result = zeros(2)
        for consumer in self.consumers:
            result = result + consumer.demand(p)

        return result

    def excessDemandGood0(self, p0):
        p = array([p0, 1.0 - p0])
        result = 0

        for consumer in self.consumers:
            result = result + consumer.excessDemand(p)[0]

        return result

    def __add__(self,other):
        try:
            return Economy(self.consumers + other.consumers)
        except:
            return Economy(self.consumers + [other])

    def numEquilibria(self, n=100):
        # p = array([p0, 1 - p0])
        q = linspace(0, 1, n)
        result = empty(len(q))
        #print result
        for i, p0 in enumerate(q):
            a = self.excessDemandGood0(p0)
            #print a
            result[i] = a
        index = zeros(len(q))

        for i in range(1, 2):
            if result[i] <= 0:  
                index[i - 1] = 1
            else: 
                index[i - 1] = 0

        for i in range(2, n - 1):
            test=result[i - 1] * result[i]
            #print test
            if test <= 0:
                index[i - 1] = 1
            else:
                index[i - 1] = 0

        for i in range(n - 2, n - 1):
            if result[i] > 0:
                index[i - 1] = 1
            else:
                index[i - 1] = 0

        count = sum(index)
        # print "The number of equilibria is"
        return count
        # print "Excess Demand funciton on the grid:"
        # print result
        # print "Index when excess demand goes from possitive to negative"
        # print index

    def __rmul__(self, other):
        economy = Economy(other * self.consumers)

        return economy

    def equilibrium(self, startbracket=None):
        def g(x):
            return self.excessDemandGood0(x)

        if startbracket == None:
            startbracket = [1e-10, 1-1e-10]

        eqPrice1 = brentq(g, startbracket[0], startbracket[1])

        return array([eqPrice1, 1 - eqPrice1])

    def __len__(self):
        return len(self.consumers)

    def __str__(self):
        resultString = 'Economy with ' + str(len(self)) + ' consumers.\n'

        for consumer in self.consumers:
            resultString = resultString + str(consumer) + '\n'

        return resultString
在实现函数
stats()
时,错误出现在最后两行代码中

产生错误的示例如下:

stats(randomEconEq(100), 100)
完整的回溯是:

>>> stats(randomEconEq(100), 100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
统计数据(randomEconEq(100),100) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:“非类型”对象不可调用
创建一个显示问题的最小示例将非常有帮助:此外,您在问题中的缩进是不正确的,如果有人试图运行它,将导致错误。如果您显示完整的traceback.UtilTheory,这也会有所帮助。uuu radd_uuuuuuuuuuu不返回值(因此隐式返回无)。我已修复源代码(等待编辑批准),这似乎对我有用。我对你的代码做了全面的分析-你有几个错误的计算(比如在
prob1=float((Eq1/iterate\u max)*100)
-应该是
prob1=100.0*Eq1/iterate\u max
)和样式问题(不要使用
str()
,使用
.format()
取而代之;不要使用camelCase;不要增量构建字符串等等),但由于调用
None
,因此不会产生
TypeError
。这完全是猜测,但从堆栈跟踪来看,执行似乎根本没有进入函数。因此,无论是
stats
还是
randomEconEq
都是
None
,它都不会更深。我打赌它是
stats
,因为它很容易在REP中重新分配L
>>> stats(randomEconEq(100), 100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable