title: Python: passing flags to functions
body: <p>For a long time i have been trying to figure out what is the best way to pass flags to python functions. The most straightforward way is something like:</p>
  
  <pre><code>def func(data, flag1, flag2, flag3):
      ...
  
  func(my_data, True, False, True)
  </code></pre>
  
  <p>This is really nice and concise, but incredibly hard to read, since the word "True" or "False" tells you nothing about what flag is being set, and you have to carefully count the arguments starting from the left. You can make them keyword arguments:</p>
  
  <pre><code>def func(data, flag1=False, flag2=False, flag3=False):
      ...
  
  func(my_data, flag1=True, flag3=True)
  </code></pre>
  
  <p>but this is kind of redundant, since the "True" doesn't carry any meaning at all. I could pass it as a list:</p>
  
  <pre><code>func(mydata, ['flag1', 'flag3'])
  </code></pre>
  
  <p>or </p>
  
  <pre><code>func(mydata, [func.flag1, func.flag3])
  </code></pre>
  
  <p>But the first feels rather dirty, using strings as flags, and the second is still somewhat repetitive. Ideally i want to say something like:</p>
  
  <pre><code>func(my_data, flag1, flag3)
  </code></pre>
  
  <p>to pass flags to a function with minimal verbosity and redundancy. Is there any way to do something like this in python?</p>
  
  <p>EDIT:
  I ended up going with:</p>
  
  <pre><code>func(mydata, flagA=1, flagB=1)
  </code></pre>
  
  <p>mostly for the reasons mentioned: compile-time checking (versus passing in strings), no namespace pollution (as opposed to using global "ENUM"s) and minimal boilerplate (=1 or =0 is only 2 characters, vs 5 or 6 for =True or =False). It also makes setting default values for the flags very easy:</p>
  
  <pre><code>def func(data, flagA=1, flagB=0, flagC=1):
      ...
  </code></pre>
  
  <p>which is far more clear and far more easy than jumping through hoops to extract and assign defaults to **kwarg-style flags. The flags are basically statically checked and very clear/clean to write. Now if only I could shave off the last two characters...</p>