How can I call a private function from some other function within the same class?
class Foo:
def __bar(arg):
#do something
def baz(self, arg):
#want to call __bar
Right now, when I do this:
__bar(val)
from baz(), I get this:
NameError: global name '_Foo__createCodeBehind' is not defined
Can someone tell me what the reason of the error is?
Also, how can I call a private function from another private function?
There is no implicit
this->in Python like you have in C/C++ etc. You have to call it onself.These methods are not really private though. When you start a method name with two underscores Python does some name mangling to make it “private” and that’s all it does, it does not enforce anything like other languages do. If you define
__baronFoo, it is still accesible from outside of the object throughFoo._Foo__bar. E.g., one can do this:This explains the “odd” identifier in the error message you got as well.
You can find it
herein the docs.