IT Consultant – Java, J2EE, IBM WebSphere Portal, Lotus Notes/Domino
RSS icon Home icon
  • Leveraging PyDev’s auto-completion for indirectly created objects

    (1 votes) 1 Star2 Stars3 Stars4 Stars5 Stars
    Loading...
    Posted on 20 February 2010 Sebastian Thomschke*/?> 5 comments

    This is just a quick tip how to enable auto completion in PyDev for indirectly created objects.

    By default, PyDev has no problems in presenting you the possible object attributes and methods on a variable if that variable gets a new object instance assigned directly in the code.

    For example, when you first type …

    mybook = Book()

    … and in the next line you enter …

    mybook.

    … PyDev will dutifully present you the statically declared features for the Book class in a popup.

    If you however get that Book instance as the result of another method, e.g. …

    mybook = bookstore.findBook("foobar")

    … PyDev currently seems to be helpless.

    Because of Python’s dynamic nature it is of course a bit harder for an IDE to figure out what kind of objects may a method. But PyDev could for example honor the @return / @rtype PyDoc annotations (which you can add to the findBook’s method declaration) but currently it just does not.

    To still have auto-completion you have two options:

    1. Temporarily instantiate a Book object in the code.
      mybook = Book()
      mybook = bookstore.findBook("foobar")
      mybook. #--> now you have auto completion
      

      This has the drawback, that if you forget to remove the first line before you finish coding an unneccessary Book instance will be created on every execution during runtime.

    2. Use an assertion to ensure that the mybook variable contains an object of type Book
      mybook = bookstore.findBook("foobar")
      assert isinstance(mybook, Book)
      mybook. #--> now you have auto completion