Edit:
My confusion has been cleared, thank you.
Instead of retreading my confusion of a very simple concept buried in the complexity of threading and multiprocess, I will just state the source of my confusion, and the simple answer to it.
I thought: self
was created BY __init__()
, so that self is INSIDE the scope of __init__()
.
In reality: self
was created before the calling of __init__()
and was created in the 'parent' scope of __init__()
. So, self
is actually a variable passed to __init__()
. In conclusion, self
is not protected and it is not special in anyway.
the code I posted below is a study in variable scoping involving threads ran by another process. while it is not related to the question anymore, it does challenge your understanding of python scoping a bit at the part: self=10 # comment out this assignment and see what happens
in def thread_WITHOUT_SelfPassedToIt():
. Thanks again.
import threading
import multiprocessing
from time import sleep
class exe_classBased(multiprocessing.Process):
def __init__(self):
super().__init__()
self.aaa = 'aaa'
self = 10
def run(self):
print(
'===================================================\n'
'<Round 0> self is NOT alterred in the scope of run()\n'
'==================================================='
)
print('self in the start of run() ==>',type(self))
def thread_WITHOUT_SelfPassedToIt():
try:
print('in a thread WITHOUT self passed to it, self==>', type(self))
except Exception as e:
print(e)
try:
print('self.aaa==',self.aaa)
except Exception as e:
print(e)
self=10 # comment out this assignment and see what happens
def thread_WITH_SelfPassedToIt(self):
print('in a thread WITH self passed to it, self==>', type(self))
try:
print('self.aaa==',self.aaa)
except Exception as e:
print(e)
t = threading.Thread(
target=thread_WITHOUT_SelfPassedToIt,
daemon=1,
)
t.start()
t = threading.Thread(
target=thread_WITH_SelfPassedToIt,
args=(self,),
daemon=1,
)
t.start()
print(
'===================================================\n'
'<Round 1> self is ALTERRED in the scope of run()\n'
'==================================================='
)
self=10
print('in the immidiate start of run() after self=10, self==>', type(self))
def thread_WITHOUT_SelfPassedToIt1():
nonlocal self
try:
print('in a thread WITHOUT self passed to it, self==>', type(self))
except Exception as e:
print(e)
self=11
def thread_WITH_SelfPassedToIt1(self):
print('in a thread WITH self passed to it, self==', self)
try:
print('self.aaa==', self.aaa)
except Exception as e:
print(e)
t = threading.Thread(
target=thread_WITHOUT_SelfPassedToIt1,
daemon=1,
)
t.start()
sleep(1)
# give the thread_WITHOUT_SelfPassedToIt enough time to have self=11 excecuted
t = threading.Thread(
target=thread_WITH_SelfPassedToIt1,
args=(self,),
daemon=1,
)
t.start()
sleep(5)
if __name__ == '__main__':
multiprocessing.freeze_support()
e = exe_classBased()
e.daemon = 1
e.start()
sleep(5)
'''
output:
===================================================
<Round 0> self is NOT alterred in the scope of run()
===================================================
self in the start of run() ==> <class '__mp_main__.exe_classBased'>
local variable 'self' referenced before assignment
local variable 'self' referenced before assignment
in a thread WITH self passed to it, self==> <class '__mp_main__.exe_classBased'>
self.aaa== aaa
===================================================
<Round 1> self is ALTERRED in the scope of run()
===================================================
in the immidiate start of run() after self=10, self==> <class 'int'>
in a thread WITHOUT self passed to it, self==> <class 'int'>
in a thread WITH self passed to it, self== 11
'int' object has no attribute 'aaa'
'''