Why does python not have a mechanism for data hiding?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 26 12:08:13 EDT 2008


En Mon, 26 May 2008 06:14:19 -0300, Paul Boddie <paul at boddie.org.uk> escribió:

>> I am also bothered a bit by the seeming inconsistency of the rules for
>> the single underscore. When used at file scope, they make the variable
>> or function invisible outside the module, but when used at class
>> scope, the "underscored" variables or functions are still fully
>> visible. For those who claim that the client should be left to decide
>> what to use, why is the client prohibited from using underscored
>> variables at file scope?
>
> I don't remember why this is. I'll leave you to track down the
> rationale for this particular behaviour. ;-)

There is no rationale because this is not how it works...
You can:
- import a module and access *all* of their names, even names prefixed with an underscore
- import any name from a module, even if it starts with an underscore
- import * from a module, and it will import all names listed in __all__, even if they start with an underscore

Only in that last case, and when the module doesn't define __all__, the list of names to be imported is built from all the global names excluding the ones starting with an underscore. And it seems the most convenient default. If one wants to access any "private" module name, any of the first two alternatives will do.

-- 
Gabriel Genellina




More information about the Python-list mailing list