title: Variable is local & global scope in a Python function
body: <p>I am learning Python and have come across a strange problem which I think is down to local / global varable scope. The problem is in this function...</p>
  
  <pre><code>def mongo_execute_command(db, database, command, inc_mongo_name):
      doc = db[database].command(command)
      if inc_mongo_name:
          doc['mongo_server'] = db.HOST + ":" + str(db.PORT)
      return doc
  </code></pre>
  
  <p>The <strong>db</strong> variable is a connection to a MongoDB server and I have other code that iterates across clusters / shards. It does this correctly, the issue lies with add the value for <strong>mongo_server</strong>. It always adds localhost:27017 regardless of the actual connection passed in. I know this because when I change the code to..</p>
  
  <pre><code>def mongo_execute_command(db, database, command, inc_mongo_name):
      doc = db[database].command(command)
      print db
      if inc_mongo_name:
          print db.HOST + ":" + str(db.PORT)
          doc['mongo_server'] = db.HOST + ":" + str(db.PORT)
      return doc
  </code></pre>
  
  <p>This is the output (annotated) when executed against a cluster..</p>
  
  <pre><code>MongoClient(u'localhost.localdomain', 30001) &lt;-- Correct, expected
  localhost:27017 &lt;- Why is this here?
  MongoClient(u'localhost.localdomain', 30002)
  localhost:27017
  MongoClient(u'localhost.localdomain', 30003)
  localhost:27017
  MongoClient(u'localhost.localdomain', 30004)
  localhost:27017
  MongoClient(u'localhost.localdomain', 30005)
  localhost:27017
  MongoClient(u'localhost.localdomain', 30006)
  localhost:27017
  </code></pre>
  
  <p>Why does 'db' contain the correct details, but when I reference db.PROPERTY, does it refer to (I assume) a global instance of that variable?</p>
  
  <p>I am using Python 2.6.6.</p>
  
  <p>Many thanks.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As Bruno correctly pointed out this is nothing to do with scope. For some reason the properties in PyMongo (3.0.3) are ignoring the provided value and setting them to defaults...</p>
  
  <pre><code>from pymongo import MongoClient
  
  c = MongoClient("localhost", 27017)
  print c
  print c.HOST
  print str(c.PORT)
  c.close()
  
  c = MongoClient("mongodb://localhost:30001/test")
  print c
  print c.HOST
  print str(c.PORT)
  
  c = MongoClient("localhost", 30001)
  print c
  print c.HOST
  print str(c.PORT)
  
  c = MongoClient(host="localhost", port=30001)
  print c
  print c.HOST
  print str(c.PORT)
  </code></pre>
  
  <p>Console output is as follows...</p>
  
  <pre><code>&gt;&gt;&gt; from pymongo import MongoClient
  &gt;&gt;&gt; 
  &gt;&gt;&gt; c = MongoClient("localhost", 27017)
  &gt;&gt;&gt; print c
  MongoClient('localhost', 27017)
  &gt;&gt;&gt; print c.HOST
  localhost
  &gt;&gt;&gt; print str(c.PORT)
  27017
  &gt;&gt;&gt; c.close()
  &gt;&gt;&gt; 
  &gt;&gt;&gt; c = MongoClient("mongodb://localhost:30001/test")
  &gt;&gt;&gt; print c
  MongoClient('localhost', 30001)
  &gt;&gt;&gt; print c.HOST
  localhost
  &gt;&gt;&gt; print str(c.PORT)
  27017
  &gt;&gt;&gt; 
  &gt;&gt;&gt; c = MongoClient("localhost", 30001)
  &gt;&gt;&gt; print c
  MongoClient('localhost', 30001)
  &gt;&gt;&gt; print c.HOST
  localhost
  &gt;&gt;&gt; print str(c.PORT)
  27017
  &gt;&gt;&gt; 
  &gt;&gt;&gt; c = MongoClient(host="localhost", port=30001)
  &gt;&gt;&gt; print c
  MongoClient('localhost', 30001)
  &gt;&gt;&gt; print c.HOST
  localhost
  &gt;&gt;&gt; print str(c.PORT)
  27017
  </code></pre>