Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
When I was working with Java, the @Override command checks the compile-time of an override. Now I was just looking for similar documentation that can indicate an override function in Python.

1 Answer

0 votes
by (108k points)

As far as I know, there is a code of the overrides decorator. This will check whether the class given as a parameter has the same function name as the method being decorated.

def overrides(interface_class):

    def overrider(method):

        assert(method.__name__ in dir(interface_class))

        return method

    return overrider

It works as follows:

class MySuperInterface(object):

    def my_method(self):

        print 'hello world!'

class ConcreteImplementer(MySuperInterface):

    @overrides(MySuperInterface)

    def my_method(self):

        print 'hello kitty!'

And if we have a faulty version means if we have a case of method overloading in the code, it will raise an assertion error during class loading:

class ConcreteFaultyImplementer(MySuperInterface):

    @overrides(MySuperInterface)

    def your_method(self):

        print 'bye bye!'

>> AssertionError!!!!!!!

 Want to become a Python Developer? Check out this insightful Python Certification course.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jan 7, 2021 in Python by ashely (50.2k points)

Browse Categories

...