About Encapsulation

  • Thread starter Adriano Monteiro
  • Start date
A

Adriano Monteiro

Hey folks,

I wanna know more about encapsulation in python. Is it fully suported?
How can I define the encapsulation statements for methods and attributes?

Regards!

[]'s!


--

Adriano Monteiro Marques
www.gopython.com.br
(e-mail address removed)

I'm FREE... Are you?
(PYTHON powered)
 
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

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 ...

.... 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
 
B

Bruno Desthuilliers

Adriano Monteiro a écrit :
Hey folks,

I wanna know more about encapsulation in python. Is it fully suported?
How can I define the encapsulation statements for methods and attributes?

If you're thinking of things like private/protected/public:

class Foo(object):
def __init__(self):
self.public = 'this is the API'
self._protected = 'implementation detail'
self.__private = 'keep out'

BTW, you may want to have a look at this:
http://c2.com/cgi/wiki?EncapsulationIsNotInformationHiding
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top