SWIG and function name question

Michael Hudson mwh21 at cam.ac.uk
Wed Nov 3 14:21:33 EST 1999

kain at twilight. (Scott Barron) writes:

> Hey folks,
> I downloaded and started to learn SWIG today and I ran into a little problem.
> In the C code there is a structure like this:
> struct __foo { ... };
> When I import my SWIG created module using 'import mymodule' I have access 
> to the struct's set/get operation via mymodule.__foo_set_member() but if I 
> import my module using 'from mymodule import *' I have access to all of my
> modules functions accept the two get/set functions that begin with __
> (I'm using dir() to check this).  This isn't too much of a problem for me
> but I'm just wondering what's going on.  Can anyone clear this up for me?

It's nothing to do with SWIG; try this for size:

put this in a file called foo.py on your PYTHONPATH:

def nowyouseeme():
    return "hi!"
def _nowyoudont():
    return "oi!"


>>> from foo import *
>>> nowyouseeme()
>>> _nowyoudont()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: _nowyoudont
>>> import foo
>>> foo.nowyouseeme()
>>> foo._nowyoudont()

Y'see, identifiers starting with `_' are considered (to some extent)
module private and are *not* imported when you use the "import *"

There's a simple answer: don't use the "import *" style. Ever.

That's a bit harsh, but the only exceptions I make are for modules
that are written with that in mine, eg. Tkinter.

Also it can be handy in interactive mode, but I still tend to avoid it
there. readline rules!


O, look: http://www.python.org/doc/current/ref/import.html has all the
gory details...

More information about the Python-list mailing list