简体   繁体   中英

Python threading module not recognizing args

For some reason, the folowing error is being thrown even though the one required argument is given.

Traceback (most recent call last):
    File "C:\Users\josep_000\Desktop\test.py", line 53, in <module>
        a=threading.Thread(target=getin(),args=(aa))
TypeError: getin() missing 1 required positional argument: 'var'

Remove the parentheses after getin() :

    a=threading.Thread(target=getin(),args=(aa))
                                   ^^ these need to be removed

Currently, your code calls getin() directly instead of passing it to the Thread constructor to be called from the context of the new thread.

Also, args needs to be a tuple. To create a single-element tuple, add a comma after aa :

    a = threading.Thread(target=getin, args=(aa,))

Python threading.Thread accepts callable object as a target.

So when you do this threading.Thread(target=getin(),args=(aa)) .. Ideally you are passing passing the return value of getin being called with no arguments. Since getin requires 1 argument this is an error.

You should pass like below...

threading.Thread(target=getin,args=(aa, ))

Also, Thread class accepts tuple as args ...

When you use parentheses without any comma inside Python just yields the value given inside....If you need a tuple with one value you've to add a comma inside.

Parentheses without any comma inside gives you a tuple object with empty items like below..

>>> a = ()
>>> a
()
>>> type(a)
<type 'tuple'>

If you use parentheses having one value inside without any comma you'll endup getting like below..

>>> b = (1)
>>> type(b)
<type 'int'>

If you need a tuple having one value you've to add a comma inside..like below.

>>> c = (1,)
>>> type(c)
<type 'tuple'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM