-1

I am having an unexpected issue in some code, and could reproduce it in a more simple example:

file1.py

class FirstClass: def function1(self): print("hello from function1") def __function2(self): print("hello from function2") 

file2.py

from file1 import FirstClass fc = FirstClass() fc.function1() fc.__function2() 

..and here's what happens:

$ python file2.py hello from function1 Traceback (most recent call last): File "file2.py", line 7, in <module> fc.__function2() AttributeError: FirstClass instance has no attribute '__function2' 

What can you do so that the call to __function2 works? I am not really supposed to go into that imported class and make that private method public.

8
  • Why is that unexpected? Commented Jan 11, 2019 at 15:51
  • Well, it is the first time it has happened to me, that's why I wasn't expecting it. Commented Jan 11, 2019 at 15:54
  • You can do it with _FirstClass__function2. If you must. I won't bother with the health warnings. See the section Private Variables here: docs.python.org/3.7/tutorial/classes.html Commented Jan 11, 2019 at 15:56
  • 1
    Note that def __somefunction is not a private method, it's used for avoiding namespace collisions, as python has no privacy model for access Commented Jan 11, 2019 at 16:02
  • You can do it like was said, but should you? Someone has modelled the class thinking that client code should not access that "private" attribute. Commented Jan 11, 2019 at 16:06

1 Answer 1

4

A function with a name starting with 2 underscore characters is not intented to be called from outside its class. And in order to allow users to redefine it in a subclass with each class calling its one (not the normal method override), its name is mangled to _className__methodName.

So here, you really should not use it directly, but if you really need to, you should be able to do:

fc._FirstClass__function2() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.