tags:

views:

35

answers:

1

Is there any way I can prevent a child class from overriding a method in the base class?

My guess is that there is not, but I'm coming from the .net world and I'm trying to make my API as robust as possible, so any input is greatly appreciated.

class Parent:
    def do_something(self):
        '''This is where some seriously important stuff goes on''' 
        pass 

class Child(Parent):
    def do_something(self):
        '''This should not be allowed.'''
        pass

Is it possible to enforce this? I know the compiler won't help, so maybe by means of some runtime check? Or is it just not a pythonian way of going about things?

+3  A: 

You are right: what you are attempting is contrary to Python's structure and its culture.

Document your API, and educate your users how to use it. It's their program, so if they still want to override your function, who are you to prevent them?

Ned Batchelder
Good point, thanks!
klausbyskov