title: Global variable values in PyCharm (Python 3.6) console
body: <p>I'm new to both Python and PyCharm, so please forgive ignorance.</p>
  
  <p>I was trying to tech myself about the execution of functions when initialising classes - specifically, I want to re-use a database connection object if passed into a new instance, but create one if not.  I have a function <code>get_cnx()</code> that creates a connection.  I discovered that, whether using a default argument in the <code>__init__</code> statement to call <code>get_cnx()</code>:</p>
  
  <pre><code>def __init__(self, db_cnx=get_cnx())
  </code></pre>
  
  <p>...or whether using a keyword argument:</p>
  
  <pre><code>self.db_cnx = kwargs.get('db_cnx', get_cnx())
  </code></pre>
  
  <p>...the function is always executed regardless of the presence (or content) of the connection argument that's passed in.  Defeats the object of re-using a connection, so I reverted to an <code>if</code> condition. I believe there's a way of doing this with a decorator, but that felt like gilding the Lilly.</p>
  
  <p>Anyway, this is the context for my actual question:  to help me work out what was going on I created this simple test, as a module called "classes.py":</p>
  
  <pre><code>greeting = 'Good Day'
  
  
  def my_func():
      global greeting
      greeting = 'Changed'
      return 'Hello'
  
  
  class Animal:
      def __init__(self, greet):
          if not greet:
              self.greet = my_func()
          else:
              self.greet = greet
  
  
  if __name__ == '__main__':
      cat = Animal(None)
  </code></pre>
  
  <p>If I run this module (with "Run with Python console" checked in the configuration), I see the global variable <code>greeting</code> shown in blue as 'Changed', which is what I'd expect.</p>
  
  <p>If I change the last bit to this:</p>
  
  <pre><code>if __name__ == '__main__':
      cat = Animal('Wotcha')
  </code></pre>
  
  <p>I see the global variable shown in blue as 'Good Day', which is also what I'd expect.</p>
  
  <p>However, when I then type this into the console:</p>
  
  <pre><code>dog = Animal(None)
  </code></pre>
  
  <p>...the global variable name turns red but still shows 'Good Day'.  </p>
  
  <p>Similarly, using the PyCharm console does the same thing:</p>
  
  <pre><code>&gt;&gt;&gt; print(greeting)
  Good Day
  &gt;&gt;&gt; dog = Animal(None)
  &gt;&gt;&gt; print(greeting)
  Good Day
  </code></pre>
  
  <p>Now, I loaded the module into IDLE and hit F5 (run module), and in the console, did this:</p>
  
  <pre><code>&gt;&gt;&gt; greeting
  'Good Day'
  &gt;&gt;&gt; dog = Animal(None)
  &gt;&gt;&gt; greeting
  'Changed'
  </code></pre>
  
  <p>This is what I would have expected to see in the PyCharm console.  </p>
  
  <p>Can someone explain what's going on?  Could it be a bug, or is it my lack of understanding of the way PyCharm deals with scope?  Or my lack of broader understanding of execution scope?</p>
  
  <p>Thanks!!</p>