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

前端 未结 10 1873
长情又很酷
长情又很酷 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:33

    Like others have said unlike Java there is not @Overide tag however above you can create your own using decorators however I would suggest using the getattrib() global method instead of using the internal dict so you get something like the following:

    def Override(superClass):
        def method(func)
            getattr(superClass,method.__name__)
        return method
    

    If you wanted to you could catch getattr() in your own try catch raise your own error but I think getattr method is better in this case.

    Also this catches all items bound to a class including class methods and vairables

提交回复
热议问题