title: Access a function variable outside the function without using "global"
body: <p>I am trying to access a local function variable outside the function in Python.</p>
  <p>I can make code like this work with global variables:</p>
  <pre><code>bye = ''
  def hi():
      global bye
      bye = 5
      sigh = 10
  
  hi()
  print(bye)
  </code></pre>
  <p>Next, I tried this code, hoping to access <code>bye</code> outside <code>hi()</code> without using <code>global bye</code>:</p>
  <pre><code>def hi():
      bye = 5 
      sigh = 10
      return
  
  hi()
  x = hi()
  print(x.bye)
  </code></pre>
  <p>This gives <code>AttributeError: 'NoneType' object has no attribute 'bye'</code>.</p>
  <p>Next, I tried:</p>
  <pre><code>def hi():
      bye = 5
      sigh = 10
      return bye
  
  hi()
  x = hi()
  print(x.bye)
  </code></pre>
  <p>This didn't improve matters; I get <code>AttributeError: 'int' object has no attribute 'bye'</code>.</p>
  <p>Is there a way to access a local function variable (<code>bye</code>) outside its function (<code>hi()</code>) without using globals and also without printing out the <code>sigh</code> variable? How can I do it?</p>