Python 使用PyDev在Eclipse中进行类型暗示
我正在学习Python,在积累了大量PHP经验之后,在Python中使用类型暗示会很方便。看起来带PyDev的Eclipse不支持这一点。有什么建议吗 例如,我希望我的IDE在使用时显示函数docstring和类型,如:Python 使用PyDev在Eclipse中进行类型暗示,python,pydev,type-hinting,Python,Pydev,Type Hinting,我正在学习Python,在积累了大量PHP经验之后,在Python中使用类型暗示会很方便。看起来带PyDev的Eclipse不支持这一点。有什么建议吗 例如,我希望我的IDE在使用时显示函数docstring和类型,如: def f(x: int) -> int: r"""Adds 3 to x""" return x + 3 f(# and now IDE shows everything about types 我不知道用Python做类型暗示的任何方法 标准的蟒
def f(x: int) -> int:
r"""Adds 3 to x"""
return x + 3
f(# and now IDE shows everything about types
我不知道用Python做类型暗示的任何方法 标准的蟒蛇练习是:
>>> def adds_three(number):
... '''Returns number + 3'''
... return number + 3
...
注意:我已经做了以下工作:
- 函数名的行为是明确的
- 参数名称很清楚应该是什么
- docstring详细说明了函数的作用
- Python是一种动态类型化语言。为什么要限制用户输入整数?浮点还支持运算符
。让他们使用它+
动态类型的一个优点是所有方法都是继承重载的。当然,您始终可以在函数中执行类型检查以防止致命错误。Python是一种动态类型化语言,不需要声明变量类型。但是,您可以在docstrings中添加有关要传递给函数的预期类型的信息,例如
def f(x):
"""
@x: int
Adds 3 to x
returns an int
"""
return x + 3
但在这种情况下,函数非常简单,我认为它不需要任何类型信息,在python中,只记录它所做的事情通常比记录严格的类型更可取
pydev确实支持docstring(但不支持类型)完成并捕获许多错误,只要您将python文件作为项目的一部分打开,而不是通过将它们拖放到Eclipse中来单独打开它们
您需要通过右键单击项目根目录,选择Properties
菜单项并选择左侧列表中的PyDev-PYTHONPATH
来添加包含python文件的文件夹,然后为包含python文件的所有文件夹单击addsourcefolder
。请注意,pydev通常可以在任何子目录中找到模块,如果其中有\uuu init\uu.py
,那么您通常只需要添加根python源文件夹
之后,在键入(
)之前,通过键入ctrl+space
访问工具提示,并在键入(
)之后通过键入ctrl+space
自动填写建议的函数参数
另请参见pydev手册(位于Present Python 2/3)
对于局部范围变量和函数参数,PyDev具有以下特性:
assert isinstance(obj, MyClass)
obj. # here hint will work
虽然我猜这是一个没有文档记录的特性。这里是PyDev和几个摘录,它们演示了Sphinx语法
class Example:
def param(self, a):
''':type a: MyClass'''
def var(self, iterable):
for a in iterable: #: :type a: AnotherClass
pass
不幸的是,这两种方法都不适用于班级成员
因为,PyDev 4也有类似于PEP-484的东西(见下文)
未来的Python 3
看看@slushy给出的答案。毫无疑问,这是未来的趋势。但目前PyDev既不支持函数注释,也不支持@slushy演示的新东西。PEP-484以某种有限的形式出现在Python 3.5中,最终出现在3.6中。以下是BDFL的类型提示和PEP-484。注释可以在Pyth中定义预期的类型这对于定义类名特别方便,因为它允许自动完成成员
epytext中的一个简单示例:
def x_intercept(m, b):
"""
Return the x intercept of the line M{y=m*x+b}. The X{x intercept}
of a line is the point at which it crosses the x axis (M{y=0}).
@type m: float
@param m: The slope of the line.
@type b: number
@param b: The y intercept of the line. The X{y intercept} of a
line is the point at which it crosses the y axis (M{x=0}).
@rtype: number
@return: the x intercept of the line M{y=m*x+b}.
"""
return -b/m
截至2014年8月,Guido Van Rossum提出了一项在函数定义中使用语法注释类型的建议,指出新语法实际上是有效的Python 3。他的建议中的一个示例(截至2014年9月尚未成为PEP)
你试过Komodo吗?不完全确定它能处理docstring,但它有intellisense。有人能确认Komodo是否满足最初的问题吗?我测试了vscode、Sublime Text、Wing IDE和Pycharm。只有Pycharm会报告
foo:str=1
(预期类型“str”改为“int”)。另外,Pycharms重构是最好的,但并不完美。“pydev不支持docstring(但不支持类型)完成”是什么意思?你能给我指出一些关于pydev从docstring推断任何方法签名的程度的文档/示例吗?感谢pydev参考,了解更多关于如何记录类型的详细信息:这个解决方案非常适合我的IDE(pycharm)。最好的答案是,pydev何时会支持PEP 484?
def x_intercept(m, b):
"""
Return the x intercept of the line M{y=m*x+b}. The X{x intercept}
of a line is the point at which it crosses the x axis (M{y=0}).
@type m: float
@param m: The slope of the line.
@type b: number
@param b: The y intercept of the line. The X{y intercept} of a
line is the point at which it crosses the y axis (M{x=0}).
@rtype: number
@return: the x intercept of the line M{y=m*x+b}.
"""
return -b/m
from typing import List, Dict
def word_count(input: List[str]) -> Dict[str, int]:
result = {} #type: Dict[str, int]
for line in input:
for word in line.split():
result[word] = result.get(word, 0) + 1
return result