kwargs - Clunky arg check in Python -
i have function:
def check_user(self, **args): allowed = ['name', 'screen_name', 'url', 'description', 'location'] arg_check = [val val in args if val not in allowed] if arg_check: raise valueerror('invalid args: ' + ' '.join(arg_check))
and works, feels unpythonic. there better way of checking this? hoping not have write big if/else statement.
this way can iterate on args in loop easily.
i think more pythonic version explicitly declare allowed arguments in function definition. replace none
default values.
def check_user(self, name=none, screen_name=none, url=none, description=none, location=none): # here # ...
Comments
Post a Comment