TypeError:/:' float'不支持的操作数类型和' instancemethod'

时间:2015-06-02 03:53:05

标签: python python-2.7

当我尝试为其他错误调试此代码时,我收到以下错误。

Traceback (most recent call last):
  File "testSweep.py", line 43, in <module>
    print sweep_operator(mesh, q)
  File "testSweep.py", line 32, in sweep_operator
    a = 2.0 * mu / dx
TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

对此有何建议?

import numpy as np
class Mesh():
    def __init__(self, num_cells, num_angles, sigma_t, sigma_s, length):
        self.num_cells = num_cells 
        self.num_angles = num_angles 
        self.sigma_t = sigma_t  
        self.sigma_s = sigma_s 
        self.length = length   

    def dx(self, dif):
    # get the width of each cell
        self.dif = self.num_cells/self.length
        return self.dif

def sweep_operator(mesh, q) :
    mu, w = np.polynomial.legendre.leggauss(2*mesh.num_angles)
    mu = mu[mesh.num_angles:]
    w  = w[mesh.num_angles:]
    phi = np.zeros(mesh.num_cells)

    for o in range(0, 2) : 
        psi_edge = np.zeros(mesh.num_angles)
        i_min = 0; i_max = mesh.num_cells; i_inc = 1
        if o :
            i_min = mesh.num_cells-1; i_max = -1; i_inc = -1
        for i in range(i_min, i_max, i_inc) :
            a = 2.0 * mu / dx
            b = 1.0 / (mesh.sigma_t + a)
            psi_center = b * (q + a[:] * psi_edge[:]) 
            psi_edge[:] = 2.0*psi_center[:] - psi_edge[:]            
            phi[i] += np.dot(psi_center, w)
    return phi


mesh = Mesh(1000, 4, 1.0, 0.5, 10)
q = 1.0
dx = mesh.dx
print sweep_operator(mesh, q)

1 个答案:

答案 0 :(得分:1)

你忘记了括号

dx = mesh.dx ()

因此,dx不是一个数字,而是一个可以调用以从网格中获取数字的东西 - 实例上的一个方法&#39;网格&#39;。