这在this thread中有点涉及,但从未解决过。在
我有一个向量类:
class Vector2D(object):
# ...
def __add__(self, other):
return Vector2D(self.x + other.x, self.y + other.y)
# ...
def __truediv__(self, scalar):
return Vector2D(self.x / scalar, self.y / scalar)
然后,我得到了一个暗示接受Vector2D的函数:
^{pr2}$如果我尝试像这样调用foo
,我会得到一个奇怪的警告,说"Expected type 'Vector2D', got 'int' instead"
:
foo((Vector2D(1, 2) + Vector2D(2, 3)) / 2)
但是,当我运行它时,它工作得很好,并且当我显式使用Vector2d
的方法时不会出现警告:
foo(Vector2D(1, 2).__add__(Vector2D(2, 3)).__truediv__(2))
请注意,我使用的是python2.7,但我在所有模块的顶部都有from __future__ import division, print_function
。谢谢你的建议。在
好的,我不能添加评论,因为我的声誉太低,但我可以创建一个答案。似乎合法。在
我试过你的代码示例(使用运算符),但没有得到警告。 甚至当我漏掉
from __future__ import division, print_function
的时候。 还有正确的操作数(或者它们叫什么?)例如,__rmul__
不生成警告。事实上,自动完成功能也可以我可以按
.
,它会显示Vector2D
类的属性。在我有PyCharm 4.5.4专业版。在
但是,您可以尝试在运算符中手动指定返回类型:
^{pr2}$您也可以尝试清除PyCharm缓存:
File -> Invalidate Caches/Restart... -> Invalidate and Restart
相关问题 更多 >
编程相关推荐