2

PEP 8 states that (emphasis mine):

We don't use the term "private" here, since no attribute is really private in Python (without a generally unnecessary amount of work).

I guess it refers to defining the actual class in some other language and then exposing only the public members to the interpreter. Is there some other way to achieve true privateness in Python?

I'm asking just out of curiosity.

Kara
  • 6,115
  • 16
  • 50
  • 57
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • @JBernardo But that doesn't _really_ make it private, does it? It just makes it harder to get to. – Paul Manta Sep 17 '11 at 17:44
  • You probably don't want to block the access... Just block the user to set your data. That's why `property` is nice. – JBernardo Sep 17 '11 at 17:52
  • @JBernardo But that's not interesting. :P As I said, I want to know out of curiosity, I like Python's "we're all consenting adults" attitude. – Paul Manta Sep 17 '11 at 17:56
  • I do not believe there is a way to achieve true privateness in python. See here: http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private – Justin Sep 17 '11 at 17:45
  • @JBernardo you still have to store the actual info somewhere, and that won't be truly private, so a property doesn't really prevent setting the data. – agf Sep 17 '11 at 23:59

3 Answers3

4

No, nothing is truly private in Python.

If you know the method name, you can get it.

I think you might come up with a clever hack, but it would be just that - a hack. No such functionality exists in the language.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • you can inspect the stack and block the access: [link](http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private/3147548#3147548) :D – JBernardo Sep 17 '11 at 17:59
  • @JBernardo, and you can mangle the stack and forge the things it checks so you get access. – habnabit Sep 17 '11 at 18:14
  • @Aaron that was just a joke... For true private methods you'll need cryptography on top of that. lol – JBernardo Sep 17 '11 at 18:16
  • @JBernardo, eh? What would cryptography do? You can still recover the method used to decrypt whatever as long as *something* can decrypt it. – habnabit Sep 17 '11 at 18:21
  • 2
    @JBernardo, as far as I understand it, Stack Overflow is a serious discussion forum for real issues. What you're saying can be misconstrued as a serious answer and is therefore not useful. – habnabit Sep 17 '11 at 18:59
1

(Note: This is not "private" in the sense of C++/C#/Java type private, but it's close)

For a class, you can prefix a variable with '__'. This will cause Python to name mangle it so you can't accidentally call it from outside the class. For example:

class Test(object):
    def __init__(self):
        self.__number = 5

a = Test()
print a.__number # name error!

On the other hand, this isn't really private. You can access the number with:

print a.__Test_number

But it will prevent accidental mistakes, which is all private should be used for anyway. If it's prefixed with '__' and someone uses the code, their fault if that code breaks later on (they were warned).

habnabit
  • 9,906
  • 3
  • 32
  • 26
Jonathan Sternberg
  • 6,421
  • 7
  • 39
  • 58
0

There is no true privateness. The best you could do is obfuscate the variable name and then create getters/setters or properties.

The only way to achieve this that I can think of this is to read/write a file every time you need to have access to that variable.

You could also write a program in another language that will only respond to certain classes. Not sure how to go about doing this, though.

Tyler Crompton
  • 12,284
  • 14
  • 65
  • 94