Hey guys,
I have some trouble understanding global variables, threading, and functions...
Code:
import threading
a=1
class myThread(threading.Thread):
def run(self):
global a
print(a)
a+=1
t=input("Specify the number of threads: ")
for b in range(t):
myThread().start()
raw_input()
#1 doubt:
class myThread(threading.Thread):
def run(self):
global a
print(a)
a+=1
Why do I have to specify 'self' while defining the function? I usually don't do it if the function isn't in a class, then why if it is in a class?
#2 doubt:
a=1
class myThread(threading.Thread):
def run(self):
global a
print(a)
a+=1
Why is variable 'a' first defined to be 1, and then globalized in the function specifically in the class? Why can't I do:
global a
a=1
class myThread(threading.Thread):
def run(self):
print(a)
a+=1
#3 doubt:
On running:
import threading
a=1
class myThread(threading.Thread):
def run(self):
global a
print(a)
a+=1
t=input("Specify the number of threads: ")
for b in range(t):
myThread().start()
raw_input()
-I get some numbers to be repeated...like if number of threads is, say 100:
.
.
.
77
78
79
80
81
82
83
84
85
86
87
87'88
89
90
.
.
.
Thanks a billion gazillion guys...