Migrating to perl?
D-Man
dsh8290 at rit.edu
Sat Jan 6 10:44:01 EST 2001
On Sat, Jan 06, 2001 at 08:21:44PM +0000, daniels at python.org wrote:
|
| Actually, that's *mostly* true but not 100%. If you have a class data
| member with a name starts with two underscores, it will basically be a
| "private" variable, accessible within the class, but not directy
| readable outside of it. Example:
|
| class X:
| def __init__(self):
| self.a = 100 # Public.
| self.__b = 200 # Private, due to the two underscores.
| def ChangeB(self, val):
| self.__b = val # This still works fine.
|
| obj = X()
| print obj.a # Prints 100
| obj.ChangeB(300) # This works fine.
| print obj.__b # Will throw an AttributeError.
As long as you realize that
print obj._X__b
will work, making private in Python not really private anyways. Of
course, this direct access will break polymorphism.
|
| So in short, you can have private data members if you want, but from
| my own experience I've learned it isn't worth my time. Part of the
| Python philosophy seems to be to Keep It Simple whenver possible, and
| if I want to keep a data member of a class private, well, I just don't
| fiddle directly with "obj.a", is all. Hope this helps.
-D
More information about the Python-list
mailing list