“列表对象不可调用”

发布于 2024-11-09 16:39:30 字数 778 浏览 0 评论 0原文

我目前正在为 Maya 编写一个基于 Python 的工具。我正在使用一行代码,该代码已在其他工具的无数其他部分中使用过,但由于某种原因,它这次拒绝工作。我看不出有什么理由不这样做。

def generateClothSkeleton(cloth):
print "Generating Cloth Skeleton"
objects = cmds.textScrollList("clothList", ai=True, q=True)
for x in range(0, len(objects)):
    numVerts = cmds.polyEvaluate((objects[x]), v=True)
    vertPosList = []
    for y in xrange(1, numVerts):
        object = objects[x]
        xformString = object + ".vtx[" + str(y) + "]"
        vertPos = cmds.xform(xformString, t=True, ws=True, a=True, q=True)
        vertPosList.extend([vertPos])
...

运行时,Python 在 object = objects[x] 行返回错误:'list' 对象不可调用。这很奇怪,考虑到没有人打电话……

你知道是什么导致了这个令人恼火的问题吗?

编辑:值得注意的是,如果我运行打印对象[x],它会按预期返回对象的名称......

I'm currently writing a Python based tool for Maya. I'm using a line of code which I have used in countless other sections of other tools, and for some reason it refuses to work this time round. I cannot see any reason why it isn't.

def generateClothSkeleton(cloth):
print "Generating Cloth Skeleton"
objects = cmds.textScrollList("clothList", ai=True, q=True)
for x in range(0, len(objects)):
    numVerts = cmds.polyEvaluate((objects[x]), v=True)
    vertPosList = []
    for y in xrange(1, numVerts):
        object = objects[x]
        xformString = object + ".vtx[" + str(y) + "]"
        vertPos = cmds.xform(xformString, t=True, ws=True, a=True, q=True)
        vertPosList.extend([vertPos])
...

When run, Python returns an error on the object = objects[x] line: 'list' object is not callable. Which is odd, considering there's no call being made...

Any ideas what's causing this infuriating issue?

EDIT: Its worth noting, that if I run print objects[x], it returns the name of the object as expected...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2024-11-16 16:39:30

这个问题已经很长时间没有答案了,所以我会尽力帮助任何试图解决类似问题的人。考虑到时间的流逝以及您没有发布堆栈跟踪或正确格式化代码的事实,我将忽略您指示代码的特定行是导致错误的原因。
首先,我将演示一些引发此错误的代码,并解释原因。

>>> a_list = []
>>> a_list()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

引发此错误的原因是变量 a_list 指向内存中的特定空列表。列表对象本身不可调用。可调用意味着您可以像函数或类构造函数一样执行它(或者是实现了 __call__ 魔术方法的对象,但这种情况并不常见)。您不执行实际的列表。

将单个项目添加到列表的正确方法是使用追加(我看到您将一个项目包装在列表中,然后使用扩展方法,这不是直接的方法,因此可能效率较低。 )

>>> a_list.append('foo')

我们可以用索引检索该项目:

>>> a_list[0]
'foo'

对于 Python 新学习者(或打字速度太快的专家)来说,早期常见的错误是使用括号而不是方括号,我们发现我们遇到了相同的错误。

>>> a_list(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

在解释器中,这个问题很快就会被捕获,但 Maya 中可能有一些东西在运行您的代码之前不会执行,因此这个问题从未被捕获。

另一种可能性是,我发现您正在覆盖内置对象。如果您尝试调用实例化对象,您会收到类似的错误。

>>> an_object = object()
>>> an_object()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'object' object is not callable

所以,我想知道您是否覆盖了列表构造函数,例如内置的 listcmds.textScrollList。如果是这种情况,请检查您的代码,看看是否存在分配如下列表的可能情况:

list = ['foo', 'bar'] # Don't do this!!!

修复它,然后重新启动您的 Python 会话。


作为帮助您理解调用列表的反例,它可以(但不应该)实现。

要真正使 list 对象可调用,请将其子类化并实现 __call__ 方法(同样,您可能不应该在生产代码中执行此操作),在这种情况下,我们扩展列出我们传递给列表的参数:

class MyList(list):
    def __call__(self, *args):
        self.extend(args)

>>> a_list = MyList(['foo', 'bar', 'baz'])
>>> a_list('ham', 'spam', 'eggs', 'spam')
['foo', 'bar', 'baz', 'ham', 'spam', 'eggs', 'spam']

不要这样做。

This Question has sat out for a long time unanswered, so I'll try to help anyone trying to solve a similar problem. Given the passage of time and the fact that you didn't post a stack-trace or format your code properly, I'll ignore that you're indicating a particular line of your code is responsible for the error.
First I'll demonstrate some code that raises this error, and explain why.

>>> a_list = []
>>> a_list()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

This error is raised because the variable a_list points to a specific empty list in memory. The list object itself isn't callable. Callable means you can execute it like a function or class constructor (or an object that has implemented the __call__ magic method, rather uncommon). You don't execute an actual list.

The proper way to add on a single item to a list is with append (I see you're wrapping an item in a list and then using the extend method, which is not the straightforward way to do it, and thus likely less efficient.)

>>> a_list.append('foo')

We can retrieve that item with an index:

>>> a_list[0]
'foo'

A common early mistake for new Python learners (or experts typing too quickly) is to use parentheses instead of brackets, and we see we get the same error.

>>> a_list(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

In an interpreter, this is caught quickly, but there is likely something in Maya that is not executed until it runs your code, and so this issue was never caught.

Another possibility, I see that you're overwriting the builtin object. You get a similar error if you attempt to call an instantiated object.

>>> an_object = object()
>>> an_object()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'object' object is not callable

So, I wonder if perhaps you've overwritten a list constructor, like the builtin list, or perhaps cmds.textScrollList. If that may be the case, look through your code for any possible cases where you're assigning a list like this:

list = ['foo', 'bar'] # Don't do this!!!

fix it, and restart your Python session.


As a counter example to help you understand calling a list, it can (but shouldn't) be implemented.

To actually make a list object callable, subclass it and implement a __call__ method (again, you probably shouldn't do it in production code), in this case we extend the list with the arguments we pass to the list:

class MyList(list):
    def __call__(self, *args):
        self.extend(args)

>>> a_list = MyList(['foo', 'bar', 'baz'])
>>> a_list('ham', 'spam', 'eggs', 'spam')
['foo', 'bar', 'baz', 'ham', 'spam', 'eggs', 'spam']

Don't do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文