title: Python: local variable referenced before assignment
body: <p>Not sure why I am getting this error</p>
  
  <p>Here is the section in my definition that is defining my variable <code>messages_outbox</code></p>
  
  <pre><code>@view_config(route_name="dashboard", request_method='GET')
  def home(self, is_desktop=True):
      callback = self.session["bounce"]
      if callback is not None:
          self.session["bounce"] = None
          return self.redirect(callback)
  
      mak = Dashboard.DesktopHomeMak
  
      member = self.member
  
      dict = {}
      # self.cls.append_preview(member=member, dict=dict)
      state = member.state
  
      messages_outbox = self.ctx.messaging.outbox
      messages_outbox = messages_outbox[:3] if len(messages_outbox) &gt; 3 else messages_outbox
      messages_outbox_total = len(messages_outbox)
  
      messages_inbox = self.ctx.messaging.inbox
      messages_inbox = messages_inbox[:3] if len(messages_inbox) &gt; 3 else messages_inbox
      messages_inbox_total = len([m for m in messages_inbox])
  
      dict['messages_outbox'] = messages_outbox
      dict['messages_outbox_total'] = messages_outbox_total
  
      dict['messages_inbox'] = messages_inbox
      dict['messages_inbox_total'] = messages_inbox_total
  
      newbie = self.ctx.session.newbie
      profile = self.member.profile
      profile = profile.deflate()
      profile["avatar"] = self.avatar_uri_large
      dict['profile'] = profile
      dict['newbie'] = newbie
      dict['verified'] = None
      dict['member'] = member
  
      if newbie is True:
          session = self.ctx.session
          try:
              state = session.state
              state.pop("newbie")
          except:
              pass
  
          try:
              session.save_state()
          except:
              pass
  
      ## if the member has not verified their email address, let them know
      if self.member.is_verified is False:
          dict['verified'] = False
      else:
          dict['verified'] = True
  
      print dict['messages_outbox']
      print messages_outbox
  
      return self.render(mak, value=dict, messages_outbox=messages_outbox, messages_inbox=messages_inbox)
  </code></pre>
  
  <p>The mak file(base.mak) is a template that pulls in another template file(home.mak) which contains the code below that is throwing the error (on the first line):</p>
  
  <pre><code>%if messages_outbox:
      % for messages_outbox in messages_outbox:
          % if messages_outbox["type"] == "Introduction":
              ${makerow_outbox(messages_outbox)}
          % endif
      % endfor
  
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td colspan="6"&gt;
                  &lt;p class="message"&gt;You don’t have any active requests.  To make a request use the &lt;a href="#" class="search-highlight"&gt;search field&lt;/a&gt;&lt;/p&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
  %endif
  </code></pre>
  
  <p>What I don't understand is that clearly <code>messages_outbox</code> is defined before I render the templates, so why am I getting this error?</p>
  
  <hr>
  
  <p>Full Traceback:</p>
  
  <pre><code>Traceback (most recent call last):
  File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/pyramid-1.4.2-py2.7.egg/pyramid/mako_templating.py", line 232, in __call__
  result = template.render_unicode(**system)
  File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/template.py", line 452, in render_unicode
  as_unicode=True)
  File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 783, in _render
  **_kwargs_for_callable(callable_, data))
  File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 815, in _render_context
  _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 841, in _exec_template
  callable_(context, *args, **kwargs)
  File "/Applications/MAMP/htdocs/WhoAt/env/www/www/templates/index/desktop/base_dashboard.mak", line 647, in render_body
  ${next.body()}
  File "/Applications/MAMP/htdocs/WhoAt/env/www/www/templates/index/desktop/home.mak", line 81, in render_body
  %if messages_outbox:
  UnboundLocalError: local variable 'messages_outbox' referenced before assignment
  </code></pre>