title: What is a clean way (or pattern) to do function parameter checking?
body: <p>Is there any design pattern or clean way to do function/method parameter checking (against allowed values)?</p>
  
  <p>Currently, lots of the initial code in my functions consists of these parameter checks (empty strings, invalid characters, existing id, ...) and, while necessary, it is a bit ugly and obfuscates the 'real' code of the function. Typically, my code goes something like this: </p>
  
  <pre><code>def my_function(p1,p2,p3,p4):
      #check parameters
      if p1 == ''
         raise InvalidArgError('p1 can not be empty')
      if p1 not in valid_list:
         raise InvalidArgError('p1 does not exist')
      if p2 &lt; 0:
         raise InvalidArgError('p2 can not be negative')
      ...
  
      #finally do something
      p = p2+p3
  </code></pre>
  
  <p>For the solution, I am thinking along the lines of <a href="http://wiki.python.org/moin/PythonDecorators#What_is_a_Decorator" rel="noreferrer">decorators</a> in Python.<br>
  I am using Python, although I guess a good solution would be language-independent.</p>