Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:13:15+00:00 2026-05-30T03:13:15+00:00

I have this funny issue. I know this error message is found in a

  • 0

I have this “funny” issue. I know this error message is found in a lot of places, but I couldn’t find one explicitely related to Mako.

In a Mako template, I have (snippet):

<%inherit file="other.mako"/>
<%def name="my_method()">Search for ${label}</%def>
[...]
<h2>${label.capitalize()} found: ${len(l)}</h2>
...
<ol>
% for (label, field_name) in some_list:
    <li>${label}: ${field_name}</li>
% endfor
</ol>

and I would get the error:

UnboundLocalError: local variable 'label' referenced before assignment

The weird part is that if I just don’t use the second ${label.capitalize()}, I don’t get any error, and the value of ${label} in the <%def> is the one I want, not the one from the for-loop. If I had the same error with the variable in <%def>, it may be clear to not reuse the same variable name, but in this case, I’m quite puzzled that such thing happens.

Can anyone tell me how I can avoid this beside renaming the variable label in the for-loop? If I rename the for-loop variable name, the problem disappear. I am transferring from another templating system that did not have this sort of error, so this similar scenario happens quite often.

Thanks for any pointers,

D.

EDIT for clarity:

I am calling my template using:

renderers.render_to_response(my_mako_tmpl,
         {'label': 'value', 'somelist':[a,b,c], 'l':[]},
         request=request)

My question is: Why the fact that I have the % for (label, field_name)-loop, the variable label is giving me and error in ${label.capitalize()}, where as it does not give me any error for Search for ${label}.

If I change my for-loop for:

% for (label_name, field_name) in some_list:

I get no error.

If I don’t change the for-loop, but I change:

<h2>${label.capitalize()} found: ${len(l)}</h2>

to

<h2>Items found: ${len(l)}</h2>

I get no error even if ${label} is used in my <%def>.

Also, to add info for the template usage, I added the <%inherit> and here is how the other.mako is defined (snippet):

<%def name="my_method()">Default value</%def>
Value of my_method() = ${self.my_method()}

So I don’t need to pass any value to my_method().

Hope this make the question more clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:13:17+00:00Added an answer on May 30, 2026 at 3:13 am

    It is best to think of a Mako template as being similar to a piece of python code. Thus, as in python, you will have issues if you expect a variable to take on both local scope and global (or nested scope).

    Thinking of a template as a function that takes variables, and defines within it various other functions including a body() function, then your problem is effectively similar to the following

    >>> def f(x):
    ...     def body():
    ...         print x
    ...         for x in range(5):
    ...             print x
    ...     body()
    ... 
    >>> f(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 6, in f
      File "<stdin>", line 3, in body
    UnboundLocalError: local variable 'x' referenced before assignment
    

    You shouldn’t expect this to work in Python, and similarly, you shouldn’t expect it to work in a Mako template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a funny issue with DB2 v9.7 and the SQL LIKE operator. Check
I have the same issue as in this post: http://social.microsoft.com/Forums/en-US/netfxbcl/thread/10027dd8-da47-4691-91a6-a8b89a7f514a Anyone know a possible
I've been searching all day and found several similar issues, but none have resolved
This is rather a funny problem I have ever come across, I have a
I have an issue where I need to find a string in a file,
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.