This is a common pitfall in Python:
tuple vs. prioritizing operationsAdditionally we have different APIs even in the standard library of Python.
Common pattern:
def caller(function, arguments, keyword_arguments)
In some newer libraries like asyncio a different pattern is used:
def caller(function,
*arguments,
**keyword_arguments)
The difference is, that in the first pattern, arguments and keyword-arguments are
not unpacked.
threading.Thread and
multiprocessing.Process follows the first pattern.
functools.partial follows the second pattern.
I see often code like this:
Syntax: Select all
thread = threading.Thread(target=some_function, args=(one_argument,)) # <- the comma is very important
If you forget the comma, it's no longer a tuple. Then it's used for prioritizing operations.
Syntax: Select all
thread = threading.Thread(target=some_function, args=(one_argument))
# exception TypeError occurs
This let you do things like:
Syntax: Select all
text = 'Hello '
name = 'All'
greeting = (text + name).upper()
print(greeting)
To visualize it better, you can use a list literal and then you don't need a comma.
Syntax: Select all
thread = threading.Thread(target=some_function, args=[one_argument])
With the original code:
If some_function takes more arguments:
With the original code:
Syntax: Select all
Delay(2, some_function, [2, 1, 3])
Btw: I don't use SourcePython, but I whish this was released in 2004.