Question #545

What is the output of the code snippet below?
class Foo(object):
    def __init__(self):
        self.__method()
    def __method(self):
        print('42')

class MoreFoo(Foo):
    def __method(self):
        print('41')

MoreFoo()

As __method() is a private method (recognisable by the double underscores __ ), it cannot be overridden by simply defining it as __method(self) in the subclass (to avoid accidental override). To force overridding, it has to be redefined as def _Foo__method(self): . This is called name mangling. (stackoverflow.com/questions/14898197/override-private-met...)


Comment on Disqus:

Question difficulty: šŸ”µšŸ”µšŸ”µšŸ”µšŸ”µ