3
class File(object):
    def __init__(self, filename):
        if os.path.isfile(filename):
            self.filename = filename
            self.file = open(filename, 'rb')
            self.__read()
        else:
            raise Exception('...')

    def __read(self):
        raise NotImplementedError('Abstract method')

class FileA(File):
    def __read(self):
        pass

file = FileA('myfile.a')

# NotImplementedError: Abstract method

My question: what's wrong? How I can fix my code to FileA use FileA.__read() to read the file instead of File.__read()? :S

Thank you in advance.

2
  • 2
    It's worth noting that implementing 'abstract methods' by having the base method raise NotImplementedError breaks multiple inheritance. Using the abc module is a much better way to do this. Commented Oct 19, 2010 at 2:40
  • @Michael Anderson: StackOverflow maintains a complete change log. You do not need to post a comment indicating you made a change. It's obvious beyond repeating. Please delete your comment. Commented Oct 19, 2010 at 2:42

2 Answers 2

7

Prefixing an attribute with double underscores doesn't make the attribute private, it simply makes polymorphism impossible because the attribute name gets mangled with the current class name. Change it to a single underscore prefix instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Better still, simply avoid all fooling around with "privacy" until you have an actual problem with someone actually corrupting your class using an attribute or method incorrectly. Until someone actually misunderstands your API, do not use any privacy constructs of any kind.
Oh my! That was it. :| I always confuse Python rules with PHP rules. Sorry for that and thank you for the answer, guys! :)
Worth noting that the under/dunder convention is useful for suggesting the difference between public methods that are going to be relatively reliable vs. private ones that may change at any moment.
The double underscore prefix isn't convention, it's syntactic sugar that results in mangling.
1

You can also leave the method undefined in the base class to achieve the same effect.

import os
class File(object):
    def __init__(self, filename):
        if os.path.isfile(filename):
            self.filename = filename
            self.file = open(filename, 'rb')
            self._read()
        else:
            raise Exception('...')
class FileA(File):
    def _read(self):
        pass
file = FileA('myfile.a')

It is invaluable to the understanding of Python classes to have this understanding of class inheritance.

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.