I have a method that accepts variable arguments and merges user supplied options with default options, eg
def run(domain, **kwargs):
options = {'opt1': 'abc', 'opt2': 'def'}
if 'opt1' in kwargs:
options['opt1'] = kwargs['opt1']
if 'opt2' in kwargs:
options['opt2'] = kwargs['opt2']
Is there any better writing than this?
You can use dictionary comprehension:
def run(domain, **kwargs):
options = {'opt1': 'abc', 'opt2': 'def'}
options = {a:kwargs.get(a, b) for a, b in options.items()}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.