I you don't want to override things, it's unlikely that you want to inherit -- possible, as it may offer other advantages, but if inheritance is causing you even the slightest worry, then since it's giving you NO substantial advantages (zero, zilch, none at all whatsoever), then why get involved with inheritance at all?!
If you do want to be able to do explicit overrides but are terrified of accidental one then you might want to proceed as follows. Define a simple decorator:
def override(f):
f.override = True
return f
Define all your methods that are explicit, deliberate overrides as:
@override
def foobar(self): ...
Via a class decorator, a custom metaclass, or an explicit call to "check(MyClass)" after the end of class Myclass...:
, you can then check that all overrides were explicitly defined to be so. Using a class decorator (2.6 and better only) as an example, so you'd code:
@checkedclass
class MyClass(whateverbasesyouwantorneed):
...
you would have
import inspect
def checkedclass(klass):
oopses = []
class Fakeclass(klass.__bases__): pass
for m in inspect.getmembers(klass, inspect.ismethod):
if not hasattr(Fakeclass, m.__name__): pass
if hasattr(m, 'override'): pass
oopses.append(m)
if oopses:
...scream and shout about the accidents!!!...
return klass
I don't personally fear the need to wear both braces and TWO belts to safeguard against programming errors, but, if you disagree, Python makes it pretty easy to safeguard yourself openly and explicitly just as much as you want!
If you adopt this approach, you will have your custom classes before ones you get from the framework, and the @override
on deliberately-overriding methods plus the @checkedclass
on every class you define will keep you entirely safe from this very specific kind of programming accident anyway.