469,898 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Private/public module members

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"?
Jul 18 '05 #1
2 1818
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" <el*******@hotmail.com> wrote in message
news:94*************************@posting.google.co m...
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"?

Jul 18 '05 #2
el*******@hotmail.com (Elbert Lev) wrote in
news:94*************************@posting.google.co m:
Not only doc(foo) has '_c', '_private' and 'b', but one can call them
from outside the module.
It this "by design"?


Yes.

All that prefixing with an underscore or listing names in '__all__' is
intended to do is to modify the behaviour of 'from foo import *'. It does
not prevent access, or even restrict visibility in any way.

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by __PPS__ | last post: by
11 posts views Thread by prefersgolfing | last post: by
86 posts views Thread by jopperdepopper | last post: by
11 posts views Thread by Yarco | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.