On Thu, 26 Mar 2015 04:43 am, Ivan Evstegneev wrote: > Hello all , > > > Just a little question about function's default arguments. > > Let's say I have this function: > > def my_fun(history=False, built=False, current=False, topo=None, > full=False, file=None): > if currnet and full: > do something_1 > elif current and file: > do something_2 > elif history and full and file: > do something_3
This is an extreme example that shows why Guido's Law "No constant arguments" is a good design principle. (Well, it's not really so much a law as a guideline.) If you have a function that looks like this: def spam(arg, flag=True): if flag: return do_this(arg) else: return do_that(arg) then you should just use do_this and do_that directly and get rid of spam. In your case, its hard to say *exactly* what you should do, since you are only showing a small sketch of "my_fun", but it looks to me that it tries to do too much. You can probably split it into two or four smaller functions, and avoid needing so many (or any!) flags. That will avoid (or at least reduce) the need to check for mutually incompatible sets of arguments. Another useful design principle: if dealing with the combinations of arguments is too hard, that's a sign that you have too many combinations of arguments. If there are combinations which are impossible, there are three basic ways to deal with that. In order from best to worst: (1) Don't let those combinations occur at all. Redesign your function, or split it into multiple functions, so the impossible combinations no longer exist. (2) Raise an error when an impossible combination occurs. (3) Just ignore one or more argument so that what was impossible is now possible. -- Steven -- https://mail.python.org/mailman/listinfo/python-list