refactoring - Python - refactor of try/except block - double call for super() method -


at beggining, sorry title of question - not came better one. suggestions welcome.

i wrote __init__ method class, works great, looks ugly. can improved? mean duplicate line calling function super().

def __init__(self, *args, **kwargs):     """     creates selenium driver.      :param args: non-keyword variables passed selenium driver.     :param kwargs: keyword variables passed selenium driver.     """      try:         phantomjs_path = 'node_modules/.bin/phantomjs'         super().__init__(phantomjs_path, *args, **kwargs)     except webdriverexception:         phantomjs_path = 'phantomjs'         super().__init__(phantomjs_path, *args, **kwargs) 

update:

try:     phantomjs_path = 'node_modules/.bin/phantomjs' except webdriverexception:     phantomjs_path = 'phantomjs' finally:     super().__init__(phantomjs_path, *args, **kwargs) 

it not work - seems obvious.

you can use loop avoid writing super line twice:

for phantomjs_path in ('node_modules/.bin/phantomjs', 'phantomjs'):     try:         super().__init__(phantomjs_path, *args, **kwargs)         break     except webdriverexception:         pass 

otherwise, there not can here. way handle exceptions in python try/except construct, take @ least 4 lines.


Popular posts from this blog

html/hta mutiple file in audio player -

debugging - Reference - What does this error mean in PHP? -