Note that there are some explanatory texts on larger screens.

plurals
  1. POthread in python vs C# vs Ruby
    primarykey
    data
    text
    <p>I think I've found something which C# and Ruby can do but Python can not</p> <p>in C#:</p> <pre><code>for (var i = 0; i &lt; 100; i++) new Thread(() =&gt; { Console.Write(i+" "); }).Start(); Console.ReadLine(); </code></pre> <p>in Ruby:</p> <pre><code>for i in 0...100 Thread.start{ print i + '' } end gets </code></pre> <p>Any ideas? Could you rewrite the above code in python? the output should be similar to C# and Ruby: <strong>has duplicate numbers</strong>.</p> <p><strong>Edit</strong><br> 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.</p> <p>sample output of C#: </p> <pre><code>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 </code></pre> <p>sample output of Ruby:</p> <pre><code>99999999999999999999999999999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999 </code></pre> <p><strong>Why do I want duplicate numbers:</strong><br> 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. <strong>Main thread and the children threads are referencing to the same variable i</strong>. 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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload