20

I'm studying Python, after a lot of PHP experience, and it would be handy to have type-hinting in Python. Looks like Eclipse with PyDev doesn't support this. Any suggestions?

For example, I want my IDE to show function docstrings and types, when I use it, like:

def f(x: int) -> int:
    r"""Adds 3 to x"""
    return x + 3

f(# and now IDE shows everything about types 
4

5 回答 5

13

呈现 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 的东西(见下文)。

class LatestExample:

  def listcase(self, param):
    ''':type param: list[str]'''

  def dictcase(self, param):
    ':type param: dict[str, MyClass]'

未来的 Python 3

看看@slushy 的回答。毫无疑问,这就是未来。但是目前 PyDev 既不支持函数注释PEP-3107也不支持@slushy 演示的新PEP-484内容。PEP-484 以某种有限的形式出现在 Python 3.5 中,并在最终版本中出现在 3.6 中。这是 BDFL 的PyCon 2015类型提示和 PEP-484 演示文稿。

于 2011-07-29T14:37:24.800 回答
13

Python 是一种动态类型语言,不需要声明变量类型。您可以在文档字符串中添加有关要传递给函数的预期类型的​​信息,例如

def f(x):
    """
    @x: int
    Adds 3 to x
    returns an int
    """
    return x + 3

但是在这种情况下,该函数非常简单,在我看来它不需要任何类型信息,并且在 python 中,仅记录它的作用通常比记录严格类型更可取。

pydev 确实支持文档字符串(但不支持类型)完成并捕获许多错误,只要您将 python 文件作为项目的一部分打开,而不是通过将它们拖放到 Eclipse 中来单独打开它们。

您需要添加包含 python 文件的文件夹,方法是右键单击项目根目录,选择Properties菜单项并选择PyDev - PYTHONPATH左侧列表,然后单击Add source folder所有包含 python 文件的文件夹。请注意,pydev 通常可以在任何子目录中找到模块(如果其中有 a __init__.py),因此您通常只需要添加根 python 源文件夹即可。

之后,您可以通过在键入ctrl+space 之前键入来访问工具提示,并通过在键入之后键入(来自动填写建议的函数参数。ctrl+space (

另请参阅http://pydev.org/manual_101_root.html上的 pydev 手册

于 2010-04-24T15:03:17.420 回答
9

截至 2014 年 8 月,Guido Van Rossum 提出在函数定义中使用mypy语法注释类型的提议,指出新语法实际上是有效的 Python 3。他提议的一个示例(截至 2014 年 9 月还不是 PEP)

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
于 2014-09-29T15:44:19.920 回答
1

我不知道在 Python 中进行类型提示的任何方法。

标准的 Pythonic 做法是这样的:

>>> def adds_three(number):
...     '''Returns number + 3'''
...     return number + 3
...     

注意我做了以下事情:

  • 函数名称的行为很明确
  • 参数名称清楚地表明它应该是什么
  • 文档字符串详细说明了函数的作用
  • Python 是一种动态类型语言。为什么限制用户输入整数?浮点数也支持运算符+。让他们使用它。

动态类型的一个好处是所有方法都是继承重载的。当然,您始终可以在函数中进行类型检查以防止致命错误。

于 2010-04-24T14:32:49.617 回答
0

reStructuredTextepytextpython3注解可以在 Python 代码中定义预期的类型,并得到pycharm等各种集成开发环境的支持。这对于定义类名特别方便,因为它允许自动完成成员。

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-07-22T07:03:10.443 回答