For example, suppose I have the code:
class Foo(object):
def bar(self, x):
'''blah blah blah'''
return x+1
And suppose I am creating another class, that has a method that takes an instance of a Foo object and does something:
class Baz(object):
def goo(self, foo):
foo.bar(1) ### I am a 'Foo' object. Auto-complete me, please
I would desperately LOVE if after typing foo. while creating the goo() method in the Baz class, the IDE would provide me with intellisense. You know, show the available methods of the Foo class, their params, their doc, etc.
In C# I would have to declare that param ‘foo’ is type ‘Foo’, so the autocompletion is trivial.
I realize that Python is dynamically typed, so there is no way for any IDE to know that the ‘foo’ parameter is of type Foo. But is there some other info, directive, or convention that some IDE can use to determine the type … strictly for the sake of intellisense and auto-completion?
One workaround I’ve found is to type Foo. which will cause most IDE’s to show intellisense for the methods in Foo. This is useful, but it’s not exactly what I’m looking for.
Another workaround is to type foo = Foo(), and then go about using foo as you normally would. Some IDE’s sniff that foo is type Foo and provide intellisense. But then you must remember to delete the line foo = Foo(), else you’ve got a nasty bug.
Perhaps, there is a much better way to accomplish what I’m trying to do. I’m using C# intellisense as a point of reference, and maybe that’s a Visual Studio thing, whereas when coding Python I should open my mind for a different way of accomplishing a similiar thing. Any suggestions would be much appreciated. I’m sure I can’t be the only one craving intellisense for Python objects.
Look at PyCharm by Jetbrains. It can auto-complete method names on an instance when the name of the referring variable matches (case-insensitive) a class name.
http://www.jetbrains.com/pycharm/
Nice screenshot here (scroll down and click the third image from the left):
http://www.jetbrains.com/pycharm/features/index.html
I should mention that this functionality is limited (because Python is dynamically typed) but it’s better than any other Python IDE I’ve tried. This, along with many other great features make it my PyCharm IDE of choice.