inheritance – Why no @override decorator in Python to help code readability?

inheritance – Why no @override decorator in Python to help code readability?

The problem with trying to add @override is that at method definition time, the decorator has no way to tell whether or not the method actually overrides another method. It doesnt have access to the parent classes (or the current class, which doesnt even exist yet!).

If you want to add @override, the @override decorator cant actually do any override checking. You then have two options. Either there is no override checking, in which case @override is no better than a comment, or the type constructor needs to specifically know about @override and check it at class creation time. A convenience feature like @override really shouldnt need to complicate core parts of the type system implementation like that. Also, if you accidentally put @override on a non-method, the bug will go undetected until you try to call the decorated function and get a weird TypeError.

Youre confusing Python decorators with Java annotations. Despite the similar syntax, they are completely different things. A Java annotation is an instruction to the compiler. But a Python decorator is executable code that does something concrete: it wraps the function in another function which can change what it does. This is the case for abstractmethod just as much as any other decorator; it does something, namely tell the ABC that there is a method that needs overriding.

inheritance – Why no @override decorator in Python to help code readability?

Leave a Reply

Your email address will not be published. Required fields are marked *