tags:

views:

151

answers:

4

in python tutorial added that python cannot hide its attributes from other classes. some thing such as private data in C++ or java..But also i know that we can use _ or __ to set some variables as privated one but it is not enogh. I think it is a weak if it is not any thing to do it.

+3  A: 

Data encapsulation in Python is enforced by convention and peer review. Surprisingly, having every attribute effectively be public hasn't caused a problem for the majority of Python programmers.

Kristo
+4  A: 

This is part of Python's philosophy. It basically trusts you to be sensible and exercise caution with anything that starts with an underscore. If you really want to hide state so no one can touch it, you can do this:

def fort_knox():
    # A very private variable
    gold = [0]

    class impl(object):
        def add_gold(self, amt):
            gold[0] += amt

        def remove_gold(self, amt):
            raise Exception('No withdrawals!')

        def count_gold(self):
            return gold[0]

    return impl()
Marcelo Cantos
`lol.fort_knox().add_gold.im_func.func_closure[0].cell_contents -> [0]`
Longpoke
@Longpoke: Cute. I guess `fort_knox` isn't as impenetrable as we've been led to believe.
Marcelo Cantos
Never raise `Exception` directly, since it can't sanely be caught. Instead, raise a more specific exception.
Mike Graham
+3  A: 

Using an underscore at the start of the name for an element or a method signals to the reader that what they're looking at is "internal implementation details". If they want to use that, they can, but it is very likely that a new version of the class will not preserve the API for internal-only method or elements (eh, "slots", I guess, the instance variables).

By having compiler-enforced guarantees as to what is and isn't visible, you are more sure that external parties are not looking at the internal bits, but even in C++ it is not that hard to access private things.

In practice, as long as you trust people not to do stupid things there's no problem having "this is internal, don't touch" as a polite reminder rather than enforced.

Vatine
Moreover, in C++ the hiding mechanism doesn't provide encapsulation against implementation changes, hence the pimpl idiom
Pete Kirkham
A: 

You are right that the _foo convention isn't sufficient to make data private; it's not supposed to be! Information hiding is not part of the design of Python. When writing programs in Python, you depend on the caller's good manners to leave your internals alone based on the naming convention and your documentation. Don't try to exert more control than this; we're all consenting adults.

There is a convention of naming internal-use methods like _foo with a single leading underscore; this serves more documentation purposes than anything else. Python name-mangles __foo attributes. Some people think this makes them more private, but it doesn't make them at all private, though it does make your classes harder to use, extend, and test. I never use them.

Mike Graham