I have tested a variety of Python IDEs for their code completion capabilities. Does one exist that can do code completion in the following case:
def foo(x):
print x.read() // remove this line, and type in "print x."
def main():
n = open("c:\\python27\\test.py");
foo(n)
The IDE has to figure out that foo is invoked somewhere in the开发者_运维技巧 current module with a parameter which was return value from a call to open() which it would have to assume is a call to file.open
, and thus, returns a file object. Thus, x.
followed by Ctrl+Space would invoke code completion and show that x, as a file object, can support any file object method, including read()
.
I have found that PyScripter, for example, can do this:
n = open("c:\\python27\\test.py");
n. // hit <ctrl+space> after n.
The above code completion scenario works because PyScripter does some special code completion logic to determine that n is a file object, but there doesn't appear to be a way to infer object types or available methods, from static analysis, that will deduce the types of parameters.
Secondly, if nothing like this is available in Python 2.x, in Python 3.x, now that there are static type hints, does any IDE support them?
def foo(x:'file'):
print x.read() // remove this line, and type in "print x." and hit ctrl+space
The only one I think can do (almost) that is PyCharm
But you must give it some hints:
If you're on Python 3, the IDE will check function annotations like that:
(The file class was removed from python 3 so I made an example with string)
It's not a free IDE but, IMHO has the best code completion available.
WingIDE has such type inference as well, it's not open though.
精彩评论