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 8374599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:57:28+00:00 2026-06-09T14:57:28+00:00

If inside a loop, is it possible to instantiate objects to be manipulated during

  • 0

If inside a loop, is it possible to instantiate objects to be manipulated during subsequent iterations of the loop, and still available when the scope of the loop has been left?

Here is a simple example of what I thought might work:

>>> for i in range(2):
...   r = [] if r is None else r
...   r.append[i]
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'r' is not defined

And here’s my second attempt using a NameError exception:

>>> for i in range(2):
...   try:
...     r = r
...   except NameError:
...     r = []
...   r.append(i)
...
>>>

I suspect that what I have been trying to do is actually prohibited, but I don’t understand why it would be.

Can someone throw some light on this for me please?

Edit:

So the second way works, but it’s very long winded. Is there a quick way?

  • 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-06-09T14:57:29+00:00Added an answer on June 9, 2026 at 2:57 pm

    You can find out if the name is in your locals like this: name in locals() Regarding your question:

    >>> 'r' in locals()
    False
    >>> for i in range(2):
        if 'r' not in locals(): r = []
        r.append(i)
    
    
    >>> r
    [0, 1]
    >>> 
    

    I agree with Ignacio Vazquez-Abrams; this is not good style. Please do what you need to before looping.

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

Sidebar

Related Questions

Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? So I've been
I am looking to do a for loop inside of Less. Is it possible
Is it possible to wait for user touch inside a while loop? I mean,
is it possible to add i to a var inside a for-loop? in wrong
Possible Duplicates: Which loop has better performance? Why? Which is optimal ? Efficiency of
Is it possible to set properties of an control inside a loop without using
Is it possible to declare global variables in MATLAB inside a loop: cellvar =
I was wondering, is it possible to run a for loop inside of a
Is it possible inside a loop to either move to next line or temporarily
Inside the initialize() function there is a jQuery each loop. Inside that loop is

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.