Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python call private parent method

I have a parent class writer that i define like this:

class writer:
    def __init__(self, file) -> None:
        self.file = file

    def write_content(self, content):
        self.file.write(content)
        self.__write_custom_footer()

    def __write_custom_footer():
        """
           Need to keep it private
        """
        # Do something

And I use this class as a parent this class:

class greetings_writer(writer):
    def __init__(self, file) -> None:
        super().__init__(file)

    def write_greetings(self):
        self.file.write("Hello")
        self.__write_custom_footer()

My problem append when i try to call the self.__write_custom_footer().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I get and AttributeError greetings_writer' object has no attribute '_greetings_writer__write_custom_footer'

For me this is due to the fact that `__write_custom_footer“is in private in the Parent class but how can I make it usable for all the child component but not from anything other?

I also tried with super().__write_custom_footer() and I got a simillar error

>Solution :

Your issue is indeed because of Python’s name mangling for private methods, which renames __write_custom_footer in writer to _writer__write_custom_footer. This makes it inaccessible directly from greetings_writer, as it’s looking for _greetings_writer__write_custom_footer.

To make the method accessible to child classes but still prevent direct access from outside, you can use a single underscore (_write_custom_footer) instead of double underscores. In Python, a single underscore conventionally signals that a method is intended for internal use (like "protected" in other languages) but is still accessible to subclasses:

class Writer:
    def __init__(self, file) -> None:
        self.file = file

    def write_content(self, content):
        self.file.write(content)
        self._write_custom_footer()  # Changed to single underscore

    def _write_custom_footer(self):
        # Do something (keep this "protected" by convention)
        pass

Now, in your greetings_writer, you can call _write_custom_footer() directly:

class GreetingsWriter(Writer):
    def __init__(self, file) -> None:
        super().__init__(file)

    def write_greetings(self):
        self.file.write("Hello")
        self._write_custom_footer()  # Accessible due to single underscore

Using a single underscore keeps the method "protected" by convention, meaning it’s intended for internal use, while still making it accessible to child classes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading