Isabel Cariod
Isabel Cariod

Reputation: 363

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?

Upvotes: 2

Views: 64

Answers (2)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

Call the Thread as:

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

Explaination:

It is throwing error in your case because args is expected to be of list or tuple type. And when your are passing "John", it is getting passed as ["J", "o", "h", "n"] i.e. array of chars

Upvotes: 2

mgilson
mgilson

Reputation: 310029

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.

Upvotes: 4

Related Questions