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

  • Home
  • SEARCH
  • 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 3333690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:49:27+00:00 2026-05-17T23:49:27+00:00

for element in container: # some code here temp_variable = f1(element) # more code

  • 0
for element in container:
  # some code here
  temp_variable = f1(element)
  # more code

# much later in the code

for element in container:
  # some code
  another_variable = g(temp_variable)
  # more code
  temp_variable = f2(element)
  # more code

In the second for loop, I accidentally used variable temp_variable before it’s assigned. Normally, I’d get NameError exception, but here unfortunately it survived, valid and initialized, from the previous loop.

Are there any coding practices, IDE tools, etc., that would help prevent such bugs?

Btw, I was thinking it might be better if variables inside the loop stayed didn’t survive past the end of the loop.

EDIT

@Ignacio Vazquez-Abrams:

If I understood correctly, you suggest not to use the same variable name as a local variable in multiple loops. I have two problems with that:

  1. Often the most descriptive variable name to use happens to be the same in multiple loops. Say, I used something like unique_visitor_count. I wouldn’t want to ban this variable from being used further down in the code, in another loop.

  2. When working on existing code, it would be quite onerous to check if any new variable name I want to use has already been used before.

  • 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-17T23:49:28+00:00Added an answer on May 17, 2026 at 11:49 pm

    If it’s ‘much later’ in the code, then you should probably break the code up into multiple functions. It sounds like the function is too long.

    Another tip is to use meaningful names. temp_variable, tmp, temp, etc. are not good names. Use a name that describes the value that it points to. This will eliminate a large class of potential occurrences of this problem.

    Also, list comprehensions don’t leak their variables so if you can use them, than that’s just one more reason that they’re generally better. They are not applicable to all situations though.

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

Sidebar

Related Questions

I am currently having trouble with the following (here is some sample code first):
I have a container element which I need to resize as its contents change.
I need to add multiple empty divs to a container element using jQuery. At
I have an input[type=radio] element inside a block container of fixed width. The supporting
I want to implement drag&drop possibilities in my application. Here is my source code:
Imagine the following two entities. Element is simple class containing some data: @Entity public
I've implemented a set of draggable elements that can be dropped into some containers
Background A project installs some files that contain all the elements to define a
How can I find out which FORM an HTML element is contained within, using
Is there a single algorithm that removes elements from a container as happens in

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.