Python 类型错误:“函数”对象没有属性“__getitem__”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13806372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:39:50  来源:igfitidea点击:

TypeError: 'function' object has no attribute '__getitem__'

pythonarraysfunction

提问by user1887919

Writing some code in python to evaluate a basic function. I've got a 2d array with some values and I want to apply the function to each of those values and get a new 2-d array:

在 python 中编写一些代码来评估基本功能。我有一个带有一些值的二维数组,我想将该函数应用于每个值并获得一个新的二维数组:

import numpy as N
def makeGrid(dim):
    ''' Function to return a grid of distances from the centre of an array.
    This version uses loops to fill the array and is thus slow.'''
    tabx = N.arange(dim) - float(dim/2.0) + 0.5
    taby = N.arange(dim) - float(dim/2.0) + 0.5
    grid = N.zeros((dim,dim), dtype='float')
    for y in range(dim):
        for x in range(dim):
            grid[y,x] = N.sqrt(tabx[x]**2 + taby[y]**2)
    return grid

import math

def BigGrid(dim):
    l= float(raw_input('Enter a value for lambda: '))
    p= float(raw_input('Enter a value for phi: '))
    a = makeGrid 
    b= N.zeros ((10,10),dtype=float) #Create an array to take the returned values
    for i in range(10):
        for j in range (10):
            b[i][j] = a[i][j]*l*p
    return b


if __name__ == "__main__":
    ''' Module test code '''
    size = 10 #Dimension of the array
    newGrid = BigGrid(size)
    newGrid = N.round(newGrid, decimals=2)
    print newGrid

But all i get is the error message

但我得到的只是错误信息

Traceback (most recent call last):
  File "sim.py", line 31, in <module>
    newGrid = BigGrid(size)
  File "sim.py", line 24, in BigGrid
    b[i][j] = a[i][j]*l*p
TypeError: 'function' object has no attribute '__getitem__'

Please help.

请帮忙。

回答by Matt Ball

It appears you have forgotten a pair of parentheses:

看来您忘记了一对括号:

a = makeGrid(dim)

What you have now:

你现在拥有的:

a = makeGrid

just aliases the makeGridfunction instead of calling it. Then, when you try to index into a, like so:

只是给makeGrid函数取别名而不是调用它。然后,当您尝试对 进行索引时a,如下所示:

a[i]

it's trying to index into a function, which does not have the __getitem__magic methodneeded for indexing with bracket notation.

它试图对一个函数进行索引,该函数没有使用括号表示法进行索引所需的魔法方法__getitem__

回答by NPE

You're not calling makeGrid(), you're assigning the function object itself to a:

您不是在调用makeGrid(),而是将函数对象本身分配给a

    a = makeGrid(dim) 

回答by Cameron Sparr

As others have said, you need to call makeGrid properly.... just as an fyi, this is a fairly common error to see in Python, and it generally means that your variable is not the type that you thought it was (in this case, you were expecting a matrix, but got a function)

正如其他人所说,您需要正确调用 makeGrid .... 仅供参考,这是在 Python 中看到的一个相当常见的错误,它通常意味着您的变量不是您认为的类型(在此情况,你期待一个矩阵,但得到了一个函数)

TypeError: 'function' object has no attribute '__getitem__'