In Python, how do I indicate I'm overriding a method?

前端 未结 10 1869
长情又很酷
长情又很酷 2020-11-29 15:43

In Java, for example, the @Override annotation not only provides compile-time checking of an override but makes for excellent self-documenting code.

I\

10条回答
  •  借酒劲吻你
    2020-11-29 16:37

    If you want this for documentation purposes only, you can define your own override decorator:

    def override(f):
        return f
    
    
    class MyClass (BaseClass):
    
        @override
        def method(self):
            pass
    

    This is really nothing but eye-candy, unless you create override(f) in such a way that is actually checks for an override.

    But then, this is Python, why write it like it was Java?

提交回复
热议问题