About Encapsulation

Sébastien Boisgérault Sebastien.Boisgerault at gmail.com
Wed May 4 08:54:43 EDT 2005


You mean private, protected, public, that kind of stuff ?

They do not exist in Python. Conventionally if you don't want
the user of a class to access a method or attribute, you use
the prefix _ ;

class K(object):

    _a = 1

    def __init__(self, val):
        self.arg = val
        self._hidden = 1

    def _method(self):
        pass


The _hidden attribute can still be accessed by ...

>>> h = K()._hidden

... but hey ! You know you *should* not. It's the
"we are all consenting adults" philosophy of
programming.

By the way, K._method and K._a won't appear
in the (py-)doc of the class ...

Cheers,

SB




More information about the Python-list mailing list