title: Setting local variable of a global function via kwargs in Python
body: <p>Not being able to overload functions in Python brought me to look into <code>*args</code> and <code>**kwargs</code> to pass undefined amounts of arguments and variable definitions to a called function. I tried to apply that concept to a simple function that prints arguments to the screen using a seperator (and without the nasty "" around everything by the while). You can find the code snippet at the end of that post. </p>
  
  <p>My problem is that I don't know the correct way to actually assign values of  kwargs to their respective parameters OUTSIDE CLASSES. If I want use <code>setattr</code> which takes in 3 arguments (<code>setatt(self, key, value)</code>) it will, of course, always give me an error when I try to pass only a key and a value. Is there a proper way to assign values to parameters for global functions outside classes?</p>
  
  <p>I hope you can clear the skies for me.</p>
  
  <p>Here the not working code example which I wanna get to work by finding an alternative to <code>setattr(self, key, value)</code>:</p>
  
  <pre><code>import sys
  
  def printAll(*args, **kwargs):
      sep = ""
      for key, value in kwargs.items():
          setattr(key, value)
      for arg in args:
          sys.stdout.write(str(arg))
          sys.stdout.write(sep)
  
  nr = 9876543210
  printAll("How many", "arguments", " I want", nr, sep='-')
  </code></pre>
  
  <p>EDIT:
  Ok, here is the updated and working example function after reading the answers of @Martijn Pieters and @Fabian:</p>
  
  <pre><code>def printAll(*args, **kwargs):
      # use sep = 'value' to define a seperator or use the default one
      sep = kwargs.get('sep')
      if sep is None:
          sep = ""
      for arg in args:
          sys.stdout.write(str(arg))
          sys.stdout.write(sep)
  </code></pre>