Migrating to perl?

daniels daniels
Sat Jan 6 15:21:44 EST 2001


On 5 Jan 2001 07:09:20 GMT, scarblac at pino.selwerd.nl (Remco Gerlich)
wrote:

[snip...]
>Joel Ricker <joejava at dragonat.net> wrote in comp.lang.python:

>> From your examples, Pythons class model is much cleaner.  Also if I
>> understand your example, variables internal to the object are kept
>> internal -- they can't be accessed without a proper method.

>In Python, nothing is ever private, you can always inspect and change
>everything. It's very dynamic. The programmer has the responsibility
>to decide when he wants to use the dynamic things.

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.

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.

--
==============================
Alan Daniels
daniels at alandaniels dot com

==============================
Alan Daniels
daniels at alandaniels dot com



More information about the Python-list mailing list