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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:14:00+00:00 2026-06-04T04:14:00+00:00

So it seems I’m misunderstanding some basic stuff about Python. Passing an instance variable

  • 0

So it seems I’m misunderstanding some basic stuff about Python. Passing an instance variable into a function should only pass a reference to the object itself, so in case the object is immutable doing something like self.var = "something"; foo(self.var) shouldn’t change self.var if foo assigned a new value to the variable – so long everything’s fine and as expected.

But now consider this:

import threading

class Test():
    def __init__(self):
        self.lock = threading.Lock()
        self.arg = "arg0"

    def foo(self, i):
        with self.lock:
            threading.Thread(target=lambda: bar(self.arg)).start()
            self.arg = "arg" + str(i)

def bar(arg):
    import time
    time.sleep(1)
    print("Bar: " + arg)

if __name__ == '__main__':
    t = Test()
    for i in range(1, 6):
        t.foo(i)

I create a thread object with a reference to the current string and afterwards update it – which the thread shouldn’t see. Thanks to the lock the next thread also should only start after the update – so while I can’t make any assumptions about the sequence in which arg0-5 will be printed I’d assume every arg should be printed exactly once. But I get the following output (Win7 x64, python 3.1 x64)

Bar: arg0
Bar: arg2
Bar: arg2
Bar: arg5
Bar: arg3

Edit: Okay after typing this up, I had the glorious idea that probably the lambda expression isn’t executed when creating the thread but later on, which would explain the behavior, so the simple workaround would be to just create a local variable and use that. Well – now that was fast help by SO 😉

  • 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-04T04:14:01+00:00Added an answer on June 4, 2026 at 4:14 am

    Since I noticed I still hadn’t answered that one, here we go:

    The lambda creates a closure to self but not to self.arg itself, which means when we later execute bar we access the newest value and not the one at lambda creation time.

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

Sidebar

Related Questions

Seems like I've ran into a wall here. I want some datasource to be
Seems this should be super simple, but I can't find the right API function
Seems it is a question about .htaccess , not sure what should I use.
Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even
Seems like this should be obvious, but how do I send arrow key presses
Seems like this should be simple, but powershell is winning another battle with me.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seems like max table comments length in mysql is only 60 characters. I'm developing
Seems like I should be able to get at it through HAT or something
seems there is some problems with global variables parsing. So {exp:addon:method url={site_url}pdf/{segment_1}} pass 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.