Hi,
I was playing around with python and noticed something:
Code:
#!/usr/bin/env python3
class PrivateParent:
def do_something(self):
self.__print_message()
def __print_message(self):
print("In Parent!")
class PrivateChild(PrivateParent):
def __print_message(self):
print("In Child!")
foo = PrivateChild()
foo.do_something()
will print out "In Parent!", while
Code:
#!/usr/bin/env python3
class PublicParent:
def do_something(self):
self.print_message()
def print_message(self):
print("In Parent!")
class PublicChild(PublicParent):
def print_message(self):
print("In Child!")
foo = PublicChild()
foo.do_something()
will print out "In Child!".
I want the second behaviour, but it doesn't really make sense to expose the method in question publicly. Is there any way around this?
Additionally, does anyone have a good explanation for why this is?
Thanks,