Is there any way to make certain variables in classes "private" (or whatever self.__var
really is) but be accessible to another class, like friends in c++, except in python? I do not want the variables in either class being messed with. Nor do I want to copy the entire code over and convert it for the second class.

- 1,439
- 1
- 16
- 29

- 8,583
- 21
- 68
- 99
-
1The double underscore prefix has nothing to do with "privacy". The name mangling is to avoid name collisions of internal instance vars in subclasses, not to prevent access. – detly Jun 14 '11 at 05:03
4 Answers
No, there is not such an option.
Use names that start with single underscores and tell the other people working on your project to not be silly about what they access.

- 507,862
- 82
- 626
- 550
The philosophy of Python is that issues like access control are up to programmer discipline. It doesn't attempt to encode in the language which parts of the program are internal implementation details, and which are part of the documented interface. Thus, it doesn't need constructs like friend
to try to declare which other parts of the program are part of the implementation of a class and which are merely clients.
The idea is that if you can't write/design/document/use good code without partially encoding these concepts into your program, you probably can't do it when you are encoding them either. Therefore it's better not to have such constructs in the language, since they don't increase the expressive power of the language and occasionally they get in the way.

- 68,572
- 20
- 126
- 174
there is no option of friend function in python. you have an option to define a protected variable by using a single underscore, but in python protected variable is also accessed by the main function, but this is not exactly the definition of a protected variable just have a look.
class Student:
_schoolName = 'XYZ School' # protected class attribute
def __init__(self, name, age):
self._name=name # protected instance attribute
self._age=age # protected instance attribute
std = Student("Swati", 25)
std._name
#answer is ->'Swati'
std._name = 'Dipa'
std._name
#answer is ->'Dipa'

- 350
- 3
- 9
I have no clue what you're talking about.
>>> class Foo(object):
... __bar = 42
...
>>> class Quux(object):
... def spam(self):
... print Foo._Foo__bar
...
>>> q = Quux()
>>> q.spam()
42

- 776,304
- 153
- 1,341
- 1,358
-
`Traceback (most recent call last): File "
", line 1, in – calccrypto Jun 14 '11 at 03:59q.spam() File " ", line 3, in spam print Foo._Foo__bar AttributeError: type object 'Foo' has no attribute '_Foo__bar'` -
Try `dir(Foo)` to see how your version of Python has mangled it. – Ignacio Vazquez-Abrams Jun 14 '11 at 04:00
-
whoops. ok youre right. however, try: `class Foo(object): def __init__(self): self.__bar = 42` – calccrypto Jun 14 '11 at 04:03
-
1Since that's an object attribute, it needs to be accessed off an object. Nothing changes except what you access it on. – Ignacio Vazquez-Abrams Jun 14 '11 at 04:13