Private/public module members

Larry Bates lbates at swamisoft.com
Thu Jun 24 09:46:06 EDT 2004


Absolutely!  The "hiding" of private attributes/methods
doesn't make them completely unreachable.  It just makes
them slightly "invisible".  There is a third method of
"hiding" attributes/methods that is done by putting two
underscores (e.g. __attribute).  This gets name mangled
to make it harder to call, but it can still be reached
if you know how.  Unlike other languages Python always
allows you to get to the attributes/methods of a class,
even when they are "private".  You should always reference
private attributes/methods with care.

Larry Bates
Syscon, Inc.

"Elbert Lev" <elbertlev at hotmail.com> wrote in message
news:9418be08.0406240537.6404a96 at posting.google.com...
> Hi, all!
>
> In accordance with Python documentation, there are 2 ways to hide
> data/methods inside the module (make them private):
>
> 1. have "public" members defined in __all__ list
> 2. start "private" members names with underscore.
>
> Both methods for some reason "behave strange".
>
> Here are 2 modules and the output:
>
> #file: main.py ########################
> import foo
> print dir(foo)
> foo.b()
> foo._c()
> foo.a()
>
> #file: foo.py######################
> import sys
> __all__ = ["a"]
> _private = 56
> def b(): print 'b'
> def _c(): print '_c'
> def a(): print 'a'
>
> Run main.py and here is the output:
>
> ['__all__', '__builtins__', '__doc__', '__file__', '__name__', '_c',
> '_private', 'a', 'b', 'sys']
> b
> _c
> a
>
> Not only doc(foo) has '_c', '_private' and 'b', but one can call them
> from outside the module.
> It this "by design"?





More information about the Python-list mailing list