Isabel Cariod Isabel Cariod - 1 year ago 66
Python Question

Python give number of args to an imported function

I'm trying to give an argument to an imported function.

I have

base.py
:

import sc1 #import sc1.py
from threading import Thread

Thread(target=sc1.main,args="John").start()


And a function in
sc1.py
:

def main(name):
print "Hello ",name


Say


TypeError: main() takes exactly 1 argument (4 given)


If I give just one
args="J"
then it works fine,

does anyone have any idea what I can do?

Answer Source

You want to pass a tuple of args:

Thread(target=sc1.main,args=("John",)).start()

In your case, since str are iterable, the Thread is trying to unpack "J" "o", "h", "n" as the arguments rather than passing the entire thing as an atomic unit.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download