Why does python not have a mechanism for data hiding?

Russ P. Russ.Paielli at gmail.com
Fri Jun 6 20:56:26 CEST 2008


On Jun 6, 8:28 am, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
> Russ P. a écrit :
>
>
>
> > On Jun 5, 2:27 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> >> On Thu, 5 Jun 2008 11:36:28 -0700 (PDT), "Russ P."
> >> <Russ.Paie... at gmail.com> declaimed the following in comp.lang.python:
>
> >>> would need to use a "mangled" name to access private data or methods.
> >>> But you will be using the name many times, you can reassign your own
> >>> name, of course, so the mangled name need not appear more than once
> >>> where it is needed.
> >>         Which will break the first time the "innards" rebind a value to the
> >> mangled name, as the "simplified" external name will still be bound to
> >> the previous value.
>
> > I'm not sure you understood what I meant. In current Python, if I need
> > access to data element __XX in class YourClass, I can use
> > ZZ._YourClass__XX, but if I don't want to clutter my code with that
> > mangled name, I can just write
>
> > XX = ZZ._YourClass__XX
>
> > and refer to it from that point on as XX.
>
> > Obviously if the meaning of
> > __XX changes within class ZZ, this will break, but that's why you are
> > supposed to avoid using private data in the first place.
>
> AFAICT, What Dennis meant is that the binding of ZZ._YourClass__XX
> changes between the moment you bind it to local XX and the moment you
> use it, then you're out.

Perhaps I should have stipulated that this should be done only in a
local scope and in an application that is not multi-threaded. Then I
don't see how you can have a problem.



More information about the Python-list mailing list