Питонический способ возврата из метода с использованием логики в переопределенном методе - PullRequest
0 голосов
/ 02 марта 2019

У меня есть унаследованный класс, который использует super() для вызова переопределенного родительского метода.Я хотел бы вернуться из дочернего метода на основе логики в родительском методе.Вот моя реализация, но мне интересно, есть ли более идиоматический способ сделать это?

class ParentClass:
    def __init__(self, parameter):
        self.parameter = parameter

    def method(self):
        if self.parameter:
            return True
        else:
            # Do more logic here
            pass

class ChildClass(ParentClass, object):
    def __init__(*args):
        ParentClass.__init__(*args)

    def method(self):
        return_from_method = super(ChildClass, self).method()
        if return_from_method:  # Is there a better way to do this?
            print('Returning from method')
            return
        else:
            print('Not returning from method')
            # Do more logic here

a = ChildClass(True)
a.method()
# > 'Returning from method'
...