WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.
Let's start with a pretty simple module - an import, a function, and a couple of classes:
(Notice by the way the semantic highlighting here - the unused parameters and variables are marked with a slight grey underline,
method names in bold, parameters in orange. More on that another day.)
Now let's go to a different file and import this module. Code completion is context dependent and shows us
which symbols are being exported by the given module. We see not only our functions and classes, the imported symbol sys
is also being defined by module, meaning that if you do from compl4 import *
you would have sys
defined as well:
Python has a way to deal with this: You can specify a variable named __all__
, which lets you define exactly what the public API for
this module should be (more details).
Let's go in and define the names we want exported by our module. I will add __all__ = [ "Bar " ]
somewhere in the file, to specify that only Bar
should be exported:
Look at the file outline / navigator on the left hand side. As soon as I typed in the __all__
line,
the icons changed to show me that only the Bar
class and its method are now "public"; there are little
lock icons on everything else! Let's see what code completion offers when we try to import this module again:
Yep, only the public API is exported now. Let's append to the __all__
list to export the func
function:
You don't have to rely on icons to tell you if something is wrong though. Let's say I've forgotten all about this, and I decided
to rename my Bar
class:
NetBeans checks that all the symbols you declare in your __all
list actually exist, and points out any discrepancies. Here, the public Bar
class no longer exists since we renamed it.
Here's another little bug I came across related to this. I was checking the __all__
handling on various library files,
in this case posixpath.py
. Notice how in the navigator, all the functions are public, except one...
Sure enough, the code looks like it's registering this conditionally added method... Do you spot the problem? There's a dot missing!
As soon as we add it, we get this:
Of course, this last bug was already detected by the
unresolved symbol detector...
No comments:
Post a Comment