0

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.

aliva
  • 4,364
  • 1
  • 28
  • 43
  • What does, "I think it is a week if it is not any thing to do it." mean? – Marcelo Cantos May 22 '10 at 12:22
  • Marcelo: I think it may be "I think it is weak if there is no way to hide information". – Vatine May 22 '10 at 12:27
  • how can hide informations from others? every one from any scope and with any permition has access to my classes attributes.is not bad? python has some instruction like c++ or java to hidding information. – aliva May 22 '10 at 12:30
  • 3
    @Marcelo Cantos: perhaps a weak point/disadvantage? Like “Not having truly private members is one of Python’s disadvantages.” (But the same holds for C++, where you can simply say `#define private public`.) – Philipp May 22 '10 at 12:35
  • 1
    @ali: Python imposes very few restrictions on the programmer. Of course you should not rely on implementation details and undocumented features, but that is true for every language. That Python does not enforce these restrictions is not generally bad, because there are always other, more complex restrictions that cannot be enforced by software. The programmer is responsible for writing sensible code, not the technology. – Philipp May 22 '10 at 12:39
  • 1
    From what evil programmer do you think you're protecting your code, and why would they have write-access to your codebase? If it's just you and your team working on it, you can surely agree not to access `_`-prefixed variables from the outside, can't you? – bobince May 22 '10 at 13:43
  • possible duplicate of [Why are Python's 'private' methods not actually private?](http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private) – Jochen Ritzel May 22 '10 at 14:28

4 Answers4

4

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.

Michael Kristofik
  • 31,476
  • 15
  • 72
  • 121
4

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
  • 167,268
  • 37
  • 309
  • 353
4

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
  • 18,928
  • 3
  • 50
  • 67
  • Moreover, in C++ the hiding mechanism doesn't provide encapsulation against implementation changes, hence the pimpl idiom – Pete Kirkham May 22 '10 at 12:46
2

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
  • 64,557
  • 13
  • 91
  • 125