About the use of **args

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Dec 10 14:25:12 CET 2003

Mark McEahern <marklists at mceahern.com> wrote in 
news:mailman.27.1071059861.9307.python-list at python.org:

> # We can still access the "private" member variable.  In Python,
> # private is merely a convention.
> print s._size

Except that in Python the convention for private variables is to begin them 
with two underscore characters, and then it is more than just a convention:

>>> class Shoe2(object):

    def __init__(self):
        self.__size = None
    def getSize(self):
        print "In getSize..."
        return self.__size
    def setSize(self, size):
        print "In setSize..."
        self.__size = size
    size = property(getSize, setSize)

>>> s2 = Shoe2()
>>> s2.size = 1
In setSize...
>>> print s2.size
In getSize...
>>> print s2.__size

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in -toplevel-
    print s2.__size
AttributeError: 'Shoe2' object has no attribute '__size'

Of course, it doesn't absolutely prevent you accessing the private variable 
if you know what you are doing, but then neither do C++ or Java:

>>> print s2._Shoe2__size

Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

More information about the Python-list mailing list