New submission from Tim Peters <t...@python.org>:

A number of contexts allow specifying a tuple of arguments to be passed later 
to a function. The Thread constructor is a fine example, and happened to come 
up (again! for me) here today:

https://stackoverflow.com/questions/69858950/why-do-we-have-to-add-comma-in-args-in-python-multithreading/69859068

This often confuses especially newbies, because the function they intend to 
parallelize often takes only a single argument, and Python's syntax for a 
1-element tuple actually _requires_ parentheses in the context of an argument 
list, with a naked trailing comma:

t = threading.Thread(target=access, args=(thread_number,))

It "looks weird" to people.

I'm not suggesting to change that, but instead to officially bless the 
workaround I've seen very often in real code: use a list instead.

t = threading.Thread(target=access, args=[thread_number])

Nobody scratches their head over what that means.

CPython's implementations typically couldn't care less what kind of sequence is 
used, and none that I'm aware of verify that it's specifically a tuple. The 
implementations just go on to do some simple variation of

    self.target(*self.args)

Tuple or list makes no real difference. I'm not really keen to immortalize the 
"any sequence type whatsoever that just happens to work" implementation 
behavior, but am keen to promise that a list specifically will work. A lot of 
code already relies on it.

----------
assignee: docs@python
components: Documentation
keywords: easy
messages: 405846
nosy: docs@python, tim.peters
priority: low
severity: normal
stage: needs patch
status: open
title: Promise that the long-time truth that `args=list` works
type: behavior
versions: Python 3.11

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45735>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to