April 2019
Intermediate to advanced
646 pages
16h 48m
English
The *args and **kwargs arguments can break the robustness of a function or method. They make the signature fuzzy, and the code often starts to become a small argument parser where it should not, for example:
def fuzzy_thing(**kwargs): if 'do_this' in kwargs: print('ok i did this') if 'do_that' in kwargs: print('that is done') print('ok')>>> fuzzy_thing(do_this=1)ok i did thisok>>> fuzzy_thing(do_that=1)that is doneok>>> fuzzy_thing(what_about_that=1)ok
If the argument list gets long and complex, it is tempting to add magic arguments. But this is more a sign of a weak function or method that should be broken into pieces or refactored.
When *args is used to deal with a sequence of elements ...