Consider the following:
class objectTest():
def __init__(self, a):
self.value = a
def get_value(self):
return self.value
class exec
Several commentators want an example of where this is useful. One application is in threading. We need to pass the target to the thread without using brackets. Otherwise the target is created in the main thread, which is what we are trying to avoid.
Example:
In test1.py I call ThreadTest without using brackets. test_thread starts in the thread and allows test1.py to continue running.
In test2.py, I pass ThreadTest() as the target. In this case the thread does not allow test2.py to continue running.
test1.py
import threading
from thread_test import ThreadTest
thread = threading.Thread(target=ThreadTest)
thread.start()
print('not blocked')
test2.py
import threading
from thread_test import ThreadTest
thread = threading.Thread(target=ThreadTest())
thread.start()
print('not blocked')
test_thread.py
from time import sleep
class ThreadTest():
def __init__(self):
print('thread_test started')
while True:
sleep(1)
print('test_thread')
output from test1.py:
thread_test started
not blocked
test_thread
test_thread
test_thread
output from test2.py:
thread_test started
test_thread
test_thread
test_thread
I am using python3.5 on Linux Mint.