tags:

views:

199

answers:

4

how can i make a methods and data members private in Python? Or doesn't python support private members??

+5  A: 

If the name of a Python function, class method, or attribute starts with (but doesn't end with) two underscores, it's private; everything else is public. Python has no concept of protected class methods (accessible only in their own class and descendant classes). Class methods are either private (accessible only in their own class) or public (accessible from anywhere).

Dive Into Python

Svetlozar Angelov
Actually this is called name mangling and the variables do not become really private. http://docs.python.org/tutorial/classes.html#private-variables
jbochi
but what if i dont want my client to use certain methods?(ie methods that are of no concern to the client,like methods used for provding a service to a public method but is of no use being public)
appusajeev
Then you're looking for "non-public" members, see answer by jbochi.
ezod
+9  A: 

9.6. Private Variables

“Private” instance variables that cannot be accessed except from inside an object, don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with classname_spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

So, for example,

class Test:
    def __private_symbol(self):
        pass
    def normal_symbol(self):
        pass

print dir(Test)

will output:

['_Test__private_symbol', 
'__doc__', 
'__module__', 
'normal_symbol']

__private_symbol should be considered a private method, but it would still be accessible through _Test__private_symbol.

jbochi
but what if i dont want my client to use certain methods?(ie methods that are of no concern to the client,like methods used for provding a service to a public method but is of no use being public)
appusajeev
@appusajeev: don't document them. if they use them it's their problem.
SilentGhost
Methods that begin with one or more underscores won't be documented, i.e., they won't show up when using `help()` on the class in the interpreter. Your client could still technically use them, but it's their own problem if they do.
mipadi
@SilentGhost i was wondering if i could provide a better abstraction
appusajeev
@mipaldi thanks for pointing that out,it almost solved my problem....
appusajeev
@appusajeev: What does "better abstraction" mean?
S.Lott
+4  A: 

The other answers provide the technical details. I'd like to emphasise the difference in philosophy between Python on one hand and languages like C++/Java (which I presume you're familiar with based on your question).

The general attitude in Python (and Perl for that matter) is that the 'privacy' of an attribute is a request to the programmer rather than a barbed wire fence by the compiler/interpreter. The idea is summarised well in this mail and is often referred to as "We're all consenting adults" since it 'assumes' that the programmer is responsible enough to not meddle with the insides. The leading underscores serve as a polite message saying that the attribute is internal.

On the other hand, if you do want to access the internals for some applications (a notable example is documentation generators like pydoc), you're free to do so. Onus is on you as a programmer to know what you're doing and do it properly rather than on the language to force you do to things it's way.

Noufal Ibrahim
+1 for "we're all consenting adults here" :-)
jbochi
+1  A: 

There are no private of any other access protection mechanisms in Python. There is a convention documented in the Python style guide for indicating to the users of your your class that they should not be accessing certain attribute.

  • _single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

  • single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g. Tkinter.Toplevel(master, class_='ClassName')

  • __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes FooBar_boo; see below).

Tendayi Mawushe