可调用函数不能真正调用吗?

时间:2018-03-10 12:19:49

标签: python function methods callable paraview

我在Ubuntu 16.04LTS下使用ParaView-5.4.1。

我有一个r类的对象PVDReader(在pvpython中,但这可能无关紧要)。 对于该类的方法TimestepValues

>>> callable(r.TimestepValues)
True

>>> type(r.TimestepValues)
<class 'paraview.servermanager.VectorProperty'>

>>> r.TimestepValues
[0.0, 0.002]

>>> r.TimestepValues()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/res/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/site-packages/paraview/servermanager.py", line 681, in __call__
    raise RuntimeError ("Cannot invoke this property")
RuntimeError: Cannot invoke this property

>>> import inspect
>>> inspect.getargspec(r.TimestepValues)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/santiago/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/inspect.py", line 816, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: [0.0, 0.002] is not a Python function

因此,一方面,它是可调用的。 另一方面,它表现得好像不是。

如何理解? 这是一个错误吗?

tl; dr PS:其他更通用的方法表现相似(但不一样)。例如,与__class__比较:

>>> callable(r.__class__)
True

>>> type(fluid_pr_reader.__class__)
<type 'type'>        <-- Rather obvious, just for completitude of the comparison with above

>>> r.__class__
<class 'paraview.servermanager.PVDReader'>

>>> r.__class__()
<paraview.servermanager.PVDReader object at 0x7f57ed56fd10>    <-- No error here

>>> inspect.getargspec(r.__class__)
Traceback (most recent call last):     <-- Same error here
  File "<stdin>", line 1, in <module>
  File "/home/res/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/inspect.py", line 816, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <class 'paraview.servermanager.PVDReader'> is not a Python function

编辑:如果我在以可调用方式访问它之前不将其作为直接属性访问,结果是相同的

>>> callable(r.TimestepValues)
True
>>> r.TimestepValues()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/res/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/site-packages/paraview/servermanager.py", line 681, in __call__
    raise RuntimeError ("Cannot invoke this property")
RuntimeError: Cannot invoke this property

0 个答案:

没有答案