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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:44:27+00:00 2026-05-24T02:44:27+00:00

I think I’ve found something which C# and Ruby can do but Python can

  • 0

I think I’ve found something which C# and Ruby can do but Python can not

in C#:

for (var i = 0; i < 100; i++)
    new Thread(() => { Console.Write(i+" "); }).Start();
Console.ReadLine();

in Ruby:

for i in 0...100
    Thread.start{
        print i + ''
    }
end

gets

Any ideas? Could you rewrite the above code in python? the output should be similar to C# and Ruby: has duplicate numbers.

Edit
The output is 100 times of print, with duplicate numbers. If you define a class or a new method, you only get 100 times of print without duplicate numbers. I know getting duplicate numbers is meaningless, I just want to use python to achieve the same effect as C# and ruby.

sample output of C#:

3 3 3 3 5 7 8 8 10 10 12 12 13 14 17 17 17 18 20 20 22 23 24 24 25 28 28 29 29 3
1 31 33 33 34 36 38 38 38 41 41 41 42 44 45 45 46 49 49 50 50 52 52 55 56 56 56
58 59 59 60 61 64 64 64 66 66 67 68 69 72 72 72 73 74 76 76 78 78 81 81 82 83 84
 84 86 86 87 89 89 90 93 94 94 95 95 97 97 99 99 100

sample output of Ruby:

99999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999

Why do I want duplicate numbers:
the output of the C# and ruby code have duplicate numbers. That’s because, the main thread are changing the value of i, so some children threads might print the same number. Main thread and the children threads are referencing to the same variable i.
Sometimes this is useful: the main thread changes the variable, the children threads all get updated variable.
But How could we do this in Python?

  • 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-24T02:44:28+00:00Added an answer on May 24, 2026 at 2:44 am

    If you want the different threads to sometimes see the same value of i then you need to pass in a mutable object and mutate it:

    import threading
    
    def action(arg):
        print arg[0],
    
    mutable = [0]
    for i in range(100):
        mutable[0] = i
        threading.Thread(target=action, args=(mutable,)).start()
    

    This has nothing whatsoever to do with whether or not you define a separate class for the thread, it is purely down to how you share the value between the threads. A global variable would work to mess up your threading in the same way.

    Sample output:

    0 1 2 3 5 5 6 8 9 9 11 11 12 1314 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 3
    0 31 32 33 34 35 36 37 39 39 40 41 43 44 44 45 46 47 48 49 50 51 53 53 54 55 56
    5758 59 60 61 62 64 65 6666 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8
    4 85 86 87 89 90 90 92 93 93 94 95 96 9898 99
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I have a very popular problem, but not found answer for it
I think they are same thing but my boss say that is not right.
I think, the following can't be done in Java. But I would be happy
I think what I need is CROSS APPLY, but I can't seem to figure
I think I got all the steps down but I must be missing something
I think this is specific to IE 6.0 but... In JavaScript I add a
I think it can be done by applying the transformation matrix of the scenegraph
I think I know how to handle this case, but I just want to
Think you are the proud owner of Facebook, then which data you want to
Think 2 entities OneToOne mapped. Person and Car. A Person can have a Car.

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.