Private attributes / Methods in Python

Gillou nospam at bigfoot.com
Tue Jan 15 09:38:01 EST 2002


AFAIK, Python has not the equivalent of  Java "private" keyword to restrict
access to methods or attributes.
By convention, all methods and attributes which name start with :

* "__" must be considered as "private" (don't use from outside of the class
body)
* "_" must be considered as "restricted" (don't use from outside the class
body or a subclass)

That's the way I'm doing private and restricted methods/attributes in
Python.
Of course, as this is only a notation but not a language feature, a
construct such "anobjectOtherThanSelf.__attribute" works but shows that this
should not be used.

--Gillou

"Henry" <henry at nospam.com> a écrit dans le message news:
uzJ08.19095$WQ1.2956322 at news6-win.server.ntlworld.com...
> HIi
>
> Could someone give an example of private variables? I have read the
> "Private Variable" section in the tutorial that you can download at the
> Python site.
>
> But I don't fully under the "class VirtualAttribute" example, because I
can
> still do
>
> a = VirtualAttribute()
> a.__setattr__("name","theValue")
>
> So, if anyone has another example (other than the VirtualAttribute
example)
> of private attributes / method in Python, that would be really good.
>
> Thanks
>





More information about the Python-list mailing list