Why does python not have a mechanism for data hiding?

Russ P. Russ.Paielli at gmail.com
Mon May 26 23:10:29 EDT 2008


On May 26, 4:23 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:

> To make things clear: _variables ARE visible when you import a module:
>
> C:\TEMP>type module.py
> _variable = 123
>
> (invoke python)
> py> import module
> py> module._variable
> 123
> py> dir(module)
> ['__builtins__', '__doc__', '__file__', '__name__', '_variable']
> py>
> py> from module import _variable
> py> _variable
> 123
>
> Only when you use "from module import *" _variable isn't imported.
> (new python session):
>
> py> from module import *
> py> _variable
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> NameError: name '_variable' is not defined

Hmmm... that seems a bit strange. Why should "_variable" be visible
when you use "import module" but not when you use "from module import
*"?

> That last form should not be used normally, except when playing with the
> interactive interpreter.

OK, I have a confession to make. I use "from module import *" almost
exclusively. But then, I'm different from most folks. I can drink six
beers and still drive safely, for example. The key is to drive faster
so I can get past dangerous situations quicker.





More information about the Python-list mailing list