<div dir="ltr">What about __setattr__()?<br><br>
<div class="gmail_quote">On Mon, Jul 28, 2008 at 5:23 AM, Nikolaus Rath <span dir="ltr"><<a href="mailto:Nikolaus@rath.org">Nikolaus@rath.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi,<br><br>Sorry for replying so late. Your MUA apparently messes up the<br>References:, so I saw you reply only now and by coincidence.<br>
<div>
<div></div>
<div class="Wj3C7c"><br>"Diez B. Roggisch" <<a href="mailto:deets@nospam.web.de">deets@nospam.web.de</a>> writes:<br>> Nikolaus Rath schrieb:<br>>> Hello,<br>>><br>>> I am really surprised that I am asking this question on the mailing<br>
>> list, but I really couldn't find it on <a href="http://python.org/doc" target="_blank">python.org/doc</a>.<br>>><br>>> Why is there no proper way to protect an instance variable from access<br>>> in derived classes?<br>
>><br>>> I can perfectly understand the philosophy behind not protecting them<br>>> from access in external code ("protection by convention"), but isn't<br>>> it a major design flaw that when designing a derived class I first<br>
>> have to study the base classes source code? Otherwise I may always<br>>> accidentally overwrite an instance variable used by the base class...<br>><br>> Here we go again...<br>><br>> <a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/188467d724b48b32/" target="_blank">http://groups.google.com/group/comp.lang.python/browse_thread/thread/188467d724b48b32/</a><br>
><br>> To directly answer your question: that's what the __ (double<br>> underscore) name mangling is for.<br><br><br></div></div>I understand that it is desirable not to completely hide instance<br>variables. But it seems silly to me that I should generally prefix<br>
almost all my instance variables with two underscores.<br><br>I am not so much concerned about data hiding, but about not<br>accidentally overwriting a variable of the class I'm inheriting from.<br>And, unless I misunderstood something, this is only possible if I'm<br>
prefixing them with __.<br><br>How is this problem solved in practice? I probably don't have a<br>representative sample, but in the libraries that I have been using so<br>far, there were a lot of undocumented (in the sense of: not being part<br>
of the public API) instance variables not prefixed with __. I have<br>therefore started to first grep the source of all base classes<br>whenever I introduce a new variable in my derived class. Is that<br>really the way it's supposed to be? What if one of the base classes<br>
introduces a new variable at a later point?<br>
<div class="Ih2E3d"><br><br>Best,<br><br> -Nikolaus<br><br>--<br> »It is not worth an intelligent man's time to be in the majority.<br> By definition, there are already enough people to do that.«<br> -J.H. Hardy<br>
<br> PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C<br><br>--<br></div>
<div>
<div></div>
<div class="Wj3C7c"><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a></div></div></blockquote></div><br><br clear="all"><br>-- <br>| _ | * | _ |<br>
| _ | _ | * |<br>| * | * | * |<br></div>