动态语言 IDE 中的自动补全,特别是 PyDev 中的 Python
我是 Python 新手,具有静态类型语言(包括大量 Java)背景。
在检查了功能/受欢迎程度等之后,我决定在 Eclipse 中使用 PyDev 作为 IDE。
令我惊讶的是,自动完成功能似乎无法正常用于内置程序。例如,如果我尝试在数据文件上自动完成:
datafile = open(directory+"/"+account, 'r') 数据文件。
没有建议有用的方法(例如实线)。仅诸如调用之类的内容。
我习惯于通过跳入类定义并使用大量自动完成来快速查看类将做什么来学习语言。我的 PyDev“解释器”通过“强制内置”设置得很好。
是否可以使用 PyDev 获得内置函数的自动完成功能?我是否错误地使用了 IDE,即应该在旁边运行一个解释器并用它来测试东西?到目前为止,IDE 似乎很弱,例如 2 分钟后我的新 Mac 上出现 IDLE 段错误。我很想知道经验丰富的 Python 开发人员在探索不熟悉的(内置)模块时会做什么,因为这让我重新考虑我最初对 Python 的吸引力。我喜欢一门可以通过轻松探索来学习的语言!
谢谢,
I'm new to Python, with a background in statically typed languages including lots and lots of Java.
I decided on PyDev in eclipse as an IDE after checking features/popularity etc.
I was stunned that auto-complete doesn't seem to work properly for builtins. For example if I try automcomplete on datafile after:
datafile = open(directory+"/"+account, 'r')
datafile.
No useful methods are suggested (e.g. realines). Only things like call.
I am used to learning a language by jumping into class definitions and using lots of auto-complete to quickly view what a class will do. My PyDev 'interpreter' is set up fine with 'forced builtins'.
Is it possible to get auto-complete for builtins with PyDev? Am I approaching the IDE wrong, i.e. should have an interpreter running on the side and test stuff with it? So far the IDEs have seemed weak, e.g. IDLE segfaulted on my new mac after 2 minutes. I'd love to know what experienced Python developers do when exploring unfamiliar (builtin) modules, as this is making me reconsider my initial attraction to Python. I like a language you can learn by easy exploration!
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在我看来,Python shell 是比依赖 IDE 更好的探索新模块的地方。不要忘记,在 Python 中,您可以在 shell 中执行任何可以在程序中执行的操作,因为没有单独的编译步骤。在 shell 中,您可以使用 dir(x) 查找 x 的所有属性和方法,无论 x 是模块、类还是其他。
更好的是,增强的 iPython shell 确实为所有对象提供了制表符补全功能。
事实上正因为如此,许多Python程序员——包括我自己——根本不使用IDE,而只是使用简单的文本编辑器(我使用VIM)。
In my opinion, the Python shell is a much better place to explore new modules than relying on an IDE. Don't forget, in Python you can do anything in the shell that you can do in a program, because there's no separate compilation step. And in the shell, you can use
dir(x)
to find all the properties and methods of x, whether x is a module, a class, or whatever.Even better, the enhanced iPython shell does provide tab completion for all objects.
In fact because of this, many Python programmers - myself included - don't use an IDE at all, but just a simple text editor (I use VIM).
只是为了保持最新状态,以便新读者不会对 Pydev 的当前状态感到困惑 - 您给出的示例现在可以在 Pydev 中运行。 (顺便说一句,应该避免手动操作路径 - 使用 os.path.join 代替)
Just to keep it up to date so that new readers are not confused about the current state of Pydev - the example you gave now works in Pydev. (btw, one should avoid operating on paths manualy - use os.path.join instead)
我使用 ipython。 Ipython 是交互式 shell 的增强版本,它添加了制表符补全和对对象的文档字符串的快速访问。它还提供了标准 shell 所没有的许多其他功能 - 您可以找到其功能的摘要 此处。
I use ipython. Ipython is an enhanced version of the interactive shell that adds tab completion and quick access to an object's doctstring. It also gives lots of other features that the standard shell does not have - you can find a summary of its features here.
这里有比较懂行的人可以给你详细的解答。这是一个简短的。
动态类型语言的自动完成功能本质上永远不会像静态类型语言那样丰富。例如,在
open
的情况下,在编写代码时无法确定返回类型是什么。与 Java 等静态类型语言不同,方法签名不包含返回类型。因此 IDE 无法给您任何提示。在任何 Python 开发过程中,您当然应该运行 REPL。解释型语言的优点之一是您可以在进行过程中在 REPL 上测试一小部分代码。这也是测试您对内置模块和其他模块如何工作的理解的好地方。
我在 Ubuntu 上工作,所以我不知道在 Mac 上运行 IDLE 有多容易或多困难。我通常使用非常方便的 iPython 来满足 REPL 需求,并使用 Pydev 进行其他开发(例如 Django)。您可能想尝试一下 iPython。
Someone more knowledgeable here can give you a detailed answer. Here is a short one.
Autocomplete for a dynamically typed language can by nature never be as rich as that for a statically typed language. In the case of
open
for instance there is no way to figure out what will be the return type at the time of writing the code. The method signature does not include a return type unlike a statically typed language like Java. Consequently the IDE is not able to give you any hints.You certainly should have an REPL running during any Python development. One advantage of an interpreted language is that you can test small chunks of your code on the REPL as you go along. It is also a good place to test your understanding of how built-ins and other modules work.
I work on Ubuntu so I do not know how easy or difficult it is to get IDLE running on a Mac. I usually work with the very handy iPython for REPL needs and use Pydev for other development (such as Django). You might want to give iPython a try.
您需要 IPython。正如 Daniel 上面指出的,交互式 shell 是探索 Python(实际上也是大多数其他语言)的更好方式。
这可能有助于在 OSX 上进行设置。
You want IPython. As Daniel pointed out above, the interactive shell is a much better way to explore Python (and indeed, most other languages too).
This might help with setting it up on OSX.
您可能想看看 WingIDE。它会正确自动完成您的数据文件。
如果无法推断类型,您可以使用断言
来帮助自动完成程序
You might want to take a look at WingIDE. It autocompletes your datafile correctly.
If it is unable to infer the type, you can use an assert like
to help the autocompleter out
我在工作中使用 PyDev,所以我知道你来自哪里。如果您愿意考虑其他工具,请查看 JetBrains 的 PyCharm,这是我的新工具我自己项目的首选 Python IDE。没有任何隶属关系可言,只是说当它结束测试时我会选择它。 :)
I use PyDev at work so I know where you're coming from. If you're willing to consider other tools, have a look at JetBrains' PyCharm, that's my new preferred Python IDE for my own projects. No affiliation to speak of except to say I'll be picking it up when it's out of beta. :)