[FRPythoneers] Leading _ in name?

Uche Ogbuji uche.ogbuji at fourthought.com
Tue May 22 07:43:07 MDT 2001


jafo at tummy.com wrote:
> 
> My impression from the discussion tonight was that something had been done
> to treat a name in a module that has a leading underscore specially to
> "hide" it.  Doesn't seem to be the case in Python 2.1 though.  What's the
> story?
> 
> f.py:
>    a = 1
>    _b = 2
>    def foo(): return(1)
>    def _bar(): return(1)
> 
> then:
>    >>> import f
>    >>> print f
>    <module 'f' from 'f.py'>
>    >>> print dir(f)
>    ['__builtins__', '__doc__', '__file__', '__name__', '_b', '_bar', 'a',
>    'foo', 'rcsid', 'revision']
>    >>> print f._b
>    2
>    >>> print f.a
>    1
>    >>> f._bar()
>    1
>    >>> f.foo()
>    1
>    >>> from f import _bar
> 
> Seems like _bar is there...

The underscores only restrict what's imported by  "from f import *":

>>> from f import *
>>> locals().keys()
['__doc__', 'a', 'foo', '__name__', '__builtins__']

Note that in 2.1, you can also do

__all__ = ["foo"]

in f.py and then "from f import *" will only import foo, not even a.


-- 
Uche Ogbuji                               Principal Consultant
uche.ogbuji at fourthought.com               +1 303 583 9900 x 101
Fourthought, Inc.                         http://Fourthought.com 
4735 East Walnut St, Ste. C, Boulder, CO 80301-2537, USA
Software-engineering, knowledge-management, XML, CORBA, Linux, Python



More information about the FRPythoneers mailing list