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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:55:43+00:00 2026-06-06T19:55:43+00:00

I really thought I understood Python variable referencing, so I’m confused why this code

  • 0

I really thought I understood Python variable referencing, so I’m confused why this code isn’t making the variable “a” be “1”:

>>> a,b,c = None, None, None
>>> for var, val in zip((a,b,c),[1]):
...     var = val
... 
>>> print a
None
>>> print var
1

Can anyone explain what Python is doing here? Why don’t “var” and “a” point to the same place?

Bonus question: What’s an elegant way to assign three variables from a list that can have 1-3 items and leave the variables set to None if the list falls short?

  • 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-06T19:55:46+00:00Added an answer on June 6, 2026 at 7:55 pm

    Can anyone explain what Python is doing here?

    It’s assigning the value of the variable val to the variable var. It is not changing a. Try the following to see what is happening:

    >>> a, b, c = None, None, None
    >>> print(zip((a,b,c), [1,2,3]))
    [(None, 1), (None, 2), (None, 3)]
    

    zip never sees the variables a, b and c, just the values they are bound to.

    Read up on Python’s use of variables (names, really) in Code Like a Pythonista.

    What’s an elegant way to assign three variables from a list that can have 1-3 items and leave the variables set to None if the list falls short?

    Don’t construct variables dynamically. Use a list instead.

    vars = (lst + [None] * 3)[:3]
    

    Then use vars[0] etc. Or

    x, y, z = (lst + [None] * 3)[:3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this really simple piece of code that I thought was the correct
I always thought I understood how this works...but lately I have started really using
I thought I really understood this, and re-reading the standard (ISO 9899:1990) just confirms
I'm not really familiar with creating generic methods, so I thought I'd put this
This post asks this question but doesn't really give an answer, so I thought
I really thought I was starting to understand how this works until I tried
I'm trying to render to a texture (really thought it would be easier than
I'm working on the datafeed script for my Affiliate program and really thought I
actually I thought I was trying something really simple. ControllerClassNameHandlerMapping sounded great to produce
I've been using scala for a while now and I thought I was really

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.