python - Short form to return method result if condition passed -
i'm wondering if there's pythonic or short-form method achieve following:
error_response = self.check_conditions(request) # if have error response, return it, otherwise continue normal. if error_response: return error_response something like:
(return self.check_conditions(request)) or pass alternatively, possible function return calling method, such as:
self.check_conditions(request) def check_conditions(self, request): error_response = do_stuff() if error_response: return_parent error_response i feeling second concept breaking ton of programming laws prevent chaos , apocalypse, thought though :)
no, there no short form conditional return.
but, second part of question:
there exceptions in python. can write this:
class myerrorresponse(exception): pass class myclass: ... def check_conditions(self, request): error_response = do_stuff() if error_response: raise myerrorresponse(error_response) def do_the_main_stuff(): try: self.check_conditions() ... except myerrorresponse e: return e.args[0]
Comments
Post a Comment