简体   繁体   English

类型错误:'builtin_function_or_method' 对象不可下标

[英]TypeError: 'builtin_function_or_method' object is not subscriptable

elif( listb[0] == "-test"):
    run_all.set("testview")
    listb.pop[0]

ERROR : Exception in Tkinter callback Traceback (most recent call last): File "/tools/python/2.7.2/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call return self.func(*args) File "./edit.py", line 581, in populate listb.pop[0] TypeError: 'builtin_function_or_method' object is not subscriptable错误:Tkinter 回调回溯中的异常(最近一次调用最后一次):文件“/tools/python/2.7.2/lib/python2.7/lib-tk/Tkinter.py”,第 1410 行,在调用中返回 self.func( *args) 文件“./edit.py”,第 581 行,在填充 listb.pop[0] 中 TypeError: 'builtin_function_or_method' 对象不可下标

The line # 581 is represented by last pop statement in the code above.第 581 行由上面代码中的最后一个 pop 语句表示。 run_all is a StringVar. run_all 是一个 StringVar。

Why am I getting this error and how can it be solved?为什么我会收到此错误以及如何解决?

I think you want我想你想要

listb.pop()[0]

The expression listb.pop is a valid python expression which results in a reference to the pop method, but doesn't actually call that method.表达式listb.pop是一个有效的 python 表达式,它导致对pop方法的引用,但实际上并不调用该方法。 You need to add the open and close parentheses to call the method.您需要添加左括号和右括号来调用该方法。

看起来您错误地输入了括号而不是括号。

instead of writing listb.pop[0] write而不是写listb.pop[0]

listb.pop()[0]
         ^
         |

You are trying to access pop as if was a list or a tupple, but pop is not.您试图将 pop 当作一个列表或元组来访问,但 pop 不是。 It's a method.这是一种方法。

Can't believe this thread was going on for so long.不敢相信这个话题持续了这么久。 You would get this error if you got distracted and used [] instead of (), at least my case.如果您分心并使用 [] 而不是 (),至少在我的情况下,您会收到此错误。

Pop is a method on the list data type, https://docs.python.org/2/tutorial/datastructures.html#more-on-lists pop 是列表数据类型上的一个方法, https://docs.python.org/2/tutorial/datastructures.html#more-on-lists

Therefore, you shouldn't be using pop as if it was a list itself, pop[0].因此,您不应该像使用列表一样使用 pop,pop[0]。 It's a method that takes an optional parameter representing an index, so as Tushar Palawat pointed out in one of the answers that didn't get approved, the correct adjustment which will fix the example above is:这是一种采用表示索引的可选参数的方法,因此正如Tushar Palawat在未获得批准的答案之一中指出的那样,将修复上述示例的正确调整是:

listb.pop(0)

If you don't believe, run a sample such as:如果您不相信,请运行一个示例,例如:

if __name__ == '__main__':
  listb = ["-test"]
  if( listb[0] == "-test"):
    print(listb.pop(0))

Other adjustments would work as well, but it feels as they are abusing the Python language.其他调整也可以,但感觉他们在滥用 Python 语言。 This thread needs to get fixed, not to confuse users.这个线程需要得到修复,而不是混淆用户。

Addition, a.pop() removes and returns the last item in the list.此外, a.pop() 删除并返回列表中的最后一项。 As a result, a.pop()[0] will get the first character of that last element.结果, a.pop()[0] 将获得最后一个元素的第一个字符。 It doesn't seem that is what the given code snippet is aiming to achieve.这似乎不是给定代码片段的目标。

This error arises when you don't use brackets with pop operation.当您不将括号与pop操作一起使用时,就会出现此错误。 Write the code in this manner.以这种方式编写代码。

listb.pop(0)

This is a valid python expression.这是一个有效的python 表达式。

FYI, this is not an answer to the post.仅供参考,这不是对帖子的回答。 But it may help future users who may get the error with the message:但它可能会帮助未来可能收到消息错误的用户:

TypeError: 'builtin_function_or_method' object is not subscriptable类型错误:'builtin_function_or_method' 对象不可下标

In my case, it was occurred due to bad indentation.就我而言,它是由于缩进不良而发生的。

Just indenting the line of code solved the issue.只需缩进代码行即可解决问题。

Mad a similar error, easy to fix:发了一个类似的错误,很容易修复:

    TypeError Traceback (most recent call last) <ipython-input-2-1eb12bfdc7db> in <module>
  3 mylist = [10,20,30] ----> 4 arr = np.array[(10,20,30)] 5 d = {'a':10, 'b':20, 'c':30} TypeError: 'builtin_function_or_method' object is not subscriptable

but I should have written it as:但我应该把它写成:

arr = np.array([10,20,30])

Very fixable, rookie/dumb mistake.非常容易修复,新手/愚蠢的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:“ builtin_function_or_method”对象不可下标 - TypeError: 'builtin_function_or_method' object is not subscriptable TypeError: 'builtin_function_or_method' object 不可下标(不使用函数时) - TypeError: 'builtin_function_or_method' object is not subscriptable (while not using a function) timm: typeError: &#39;builtin_function_or_method&#39; 对象不可下标 - timm: typeError: 'builtin_function_or_method' object is not subscriptable TypeError: 'builtin_function_or_method' object 不可用于 python - TypeError: 'builtin_function_or_method' object is not subscriptable for python Python:TypeError:“ builtin_function_or_method”对象不可下标: - Python: TypeError: 'builtin_function_or_method' object is not subscriptable: TypeError:&#39;builtin_function_or_method&#39;对象不可订阅 - 查找素数:/ - TypeError: 'builtin_function_or_method' object is not subscriptable - finding primes :/ Python 2.7: TypeError: 'builtin_function_or_method' object 不可订阅 - Python 2.7 : TypeError: 'builtin_function_or_method' object is not subscriptable Python“TypeError:&#39;builtin_function_or_method&#39;对象不可订阅” - Python “TypeError: 'builtin_function_or_method' object is not subscriptable" TypeError: 'builtin_function_or_method' object is not subscriptable error in python 3 - TypeError: 'builtin_function_or_method' object is not subscriptable error in python 3 导入数学和 TypeError: 'builtin_function_or_method' object 不可下标 - Importing math and TypeError: 'builtin_function_or_method' object is not subscriptable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM