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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:19:00+00:00 2026-05-28T11:19:00+00:00

I want to do the following: a = 1 b = 2 c =

  • 0

I want to do the following:

a = 1
b = 2
c = 3
tom = [a,b,c]
for i in tom:
    i = 6

The desired result is a = 6

The actual result is a = 1

I’m guessing that there is no way to do this without some kind of exec. Correct?

  • 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-28T11:19:01+00:00Added an answer on May 28, 2026 at 11:19 am

    Initially I misunderstood your question, and I see that kindall got it right. But I think this question shows a need for a more detailed explanation of how Python works. The best way to think about variables in Python is to think of variable names as having arrows attached to them, and values as objects to which those arrows point. Variable names point to objects. So when you do this:

    a = 1
    

    You’re really saying “a points to 1“. And when you do this:

    b = a
    

    You’re really saying “b points to the same object as a“. Under normal circumstances, a variable can’t point to another variable name; it can only point at the same object that the other variable points at. So when you do this:

    tom = [a, b, c]
    

    You aren’t creating a list that points to the variable names a, b, and c; you’re creating a list that points to the same objects as a, b, and c. If you change where a points, it has no effect on where tom[0] points. If you change where tom[0] points, it has no effect on where a points.

    Now, as others have pointed out, you can programmatically alter the values of variable names, ether using exec as you suggested (not recommended), or by altering globals() (also not recommended). But most of the time, it’s just not worth it.

    If you really want to do this, my suggestion would be either simply to use a dictionary (as suggested by DzinX) or, for a solution that’s closer to the spirit of your question, and still reasonably clean, you could simply use a mutable object. Then you could use getattr and setattr to programmatically alter the attributes of that object like so:

    >>> class Foo():
    ...     pass
    ... 
    >>> f = Foo()
    >>> f.a = 1
    >>> setattr(f, 'b', 2)
    >>> getattr(f, 'a')
    1
    >>> f.b
    2
    

    Generally, the best solution is to just use a dictionary. But occasionally situations might arise in which the above is better.

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

Sidebar

Related Questions

I've got some code like the following. I want it so that when I
i want the following functionality. input : this is test <b> bold text </b>
Is there a language agnostic algorithm to name tuples? Specifically, I want the following
The problem is following: I want to automate the way my emacs starts. It
With win32api, I want that the following program creates two process and creates a
I want to ran the following script on text files that are being committed:
I have a result set of data that I want to write to an
I was following this example: http://developer.android.com/resources/tutorials/views/hello-autocomplete.html And I want to know how I can
I want the following layout to appear on the screen: FieldName 1 [Field input
I want the following functionality using php I have a csv file. Each file

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.