tags:

views:

123

answers:

3

Suppose I have this function signature:

def foo(a=True, b=True, c=True, d=True, e=True):

I've decided these would be concise ways to call this function, considering all passed parameters should be False:

foo(*5*[False])
foo(*[False]*5)

But something tells me that would be bad Python style. What do you think?

+8  A: 

if it's hard to read, it's bad style.

remember that code is read a lot more often than written.

Javier
Yeah, at first I thought that wasn't so hard to read and understand, but then I thought again. Thanks!
guinaps
+1  A: 

Your code should explain itself and be easy to read. The Style Guide for Python gives this example which should help answer your question:

Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.

startswith() and endswith() are cleaner and less error prone. For

example:

    Yes: if foo.startswith('bar'):

    No:  if foo[:3] == 'bar':
0A0D
+2  A: 

I like following the 80-20 rule with default values. If a function has default values for a parameter and I choose to use a different value, I need to make that evident in the call because that is an important piece of information.

If you are actually going and "violating" five default parameters, I would explicitly show that in the call to make it clear to the reader. i.e., I would explicitly list each parameter by name and indicate the false.

While the code you wrote should work, it is distracting, and someone would focus on figuring out what the hell you are doing rather than realizing the important bit - that you are making a very special call because you are not taking any of the defaults. This is not the place to save (electronic) trees.

Uri