在 Eclipse 中使用 PyDev 进行类型提示 [英] Type hinting in Eclipse with PyDev

查看:47
本文介绍了在 Eclipse 中使用 PyDev 进行类型提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在积累了大量 PHP 经验之后,我正在学习 Python,在 Python 中使用 类型提示 会很方便.看起来带有 PyDev 的 Eclipse 不支持这个.有什么建议么?

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?

例如,我希望我的 IDE 在使用时显示函数 docstringstypes,例如:

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 

推荐答案

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

Python is a dynamically-typed language, where variable types don't need to be declared. You can add information about the expected types intended to be passed to the function in docstrings though, e.g.

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

但在这种情况下,该函数非常简单,在我看来它不需要任何类型信息,并且只记录它的作用在 Python 中通常比记录严格类型更受欢迎.

But in this case, the function is so simple it doesn't need any type info in my opinion and just documenting what it does is often preferred in python over documenting strict types.

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

pydev does support docstring (but not types) completion and catches many errors, so long as you're opening the python files as part of a project and not opening them separately by drag-dropping them into Eclipse.

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

You need to add the folders containing python files by right clicking on the project root, selecting the Properties menu item and selecting PyDev - PYTHONPATH on the list on the left, and clicking Add source folder for all the folders with python files. Note that pydev can usually find modules in any subdirectories if there's a __init__.py in them, so you often only need to add the root python source folder.

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

After that, you access the tooltips by typing ctrl+space before typing the (, and auto-fill out suggested function arguments by typing ctrl+space after typing (.

另请参阅 http://pydev.org/manual_101_root.html

这篇关于在 Eclipse 中使用 PyDev 进行类型提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆