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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:05:17+00:00 2026-06-10T23:05:17+00:00

This question is related to my earlier question here: python update outer passed value

  • 0

This question is related to my earlier question here: python update outer passed value from within a for loop.

Coming from a Perl background it has never been a problem to pass a variable by reference and update the value from within a child scope as in the for-loop example below:

#!/usr/bin/perl
my ($str1,$str2) = ('before','before');
print "before - str1:'$str1', str2:'$str2'\n";

foreach my $str($str1,$str2){$str = 'after'}
print "after - str1:'$str1', str2:'$str2'\n";

I understand in Python this is not possible as variables are imported by-value rather than by-reference, the only solution I’ve found within Python so far which achieves exactly what I need is:

def changeValue(str):
  return 'after'

str1 = 'before'
str2 = 'before'
for s in str1,str2: print s

str1 = changeValue(str1)
str2 = changeValue(str2)
for s in str1,str2: print s

Although not ideal this would be OK if I could make the function ‘changeValue’ calls from within a for-loop – rather than calling them individually as above – but then I am back to the original “can’t pass a variable by reference” problem.

I am sure that there must be a simple and less convoluted Pythonic way to achieve what I’m after?

  • 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-10T23:05:19+00:00Added an answer on June 10, 2026 at 11:05 pm

    In python, the way to do something like this is almost certainly to use a dictionary to store your data:

    d = {'s1':'before','s2':'before'}
    
    for k in d.keys():
        print d[k]
    
    for k in d.keys():
        d[k] = 'after'
        print d[k]
    

    There are other solutions (modifying the globals dictionary works, but is HIGHLY DISCOURAGED AS IT LEADS TO CODE WHICH IS ALMOST IMPOSSIBLE TO READ/DEBUG). Perhaps if we knew the actual use-case for this, we might be able to provide some better advice.

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

Sidebar

Related Questions

This Question is Related to my earlier post here: CSS loading issue with Android
This question is related to my earlier question, asked here: How do I get
This question is related with one of my earlier questions.. Previous Post In there
This is related to this question I asked earlier about syntax highlighting user-defined blocks
This is a related question to one I posted earlier... I'm trying to sum
This is a related question to one I posted earlier today, I was initially
This is related to an earlier question by a different user, asking How to
This is related to my earlier question. ren C:\Temp\%%A %%A if errorlevel 0 (
This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question is related to the one I asked here . I'm trying to

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.