title: Global "connection"-like variables in Python
body: <p>Full script: <a href="https://gist.github.com/4476526" rel="nofollow">https://gist.github.com/4476526</a></p>
  
  <p>The specific code in question is</p>
  
  <pre><code># Cloud Files username &amp; API key
  username = ''
  key = ''
  
  # Source and destination container names
  originContainerName = ''
  targetContainerName = ''
  
  ...
  
  def cloudConnect():
      global originContainer
      global targetContainer
      global connection
      print "Creating connection"
      connection = cloudfiles.get_connection(username,key,servicenet=True)
      print "-- [DONE]"
      print "Accessing containers"
      originContainer = connection.create_container(originContainerName)
      targetContainer = connection.create_container(targetContainerName)
      print "-- [DONE]"
      return
  </code></pre>
  
  <p>The script works perfectly fine, however I've read in multiple places that global variables should be used with hesitation and that there is almost always a better way to do the same thing without them. Is this true? And if so, how exactly should I fix this script? To me it seems much easier just to use global connection and container variables instead of passing those objects around as arguments in multiple functions.</p>