Reputation: 711
according to me, weirdest problem that took me forever to figure out because I simply can't fathom what is wrong, anyway here goes:
class pod_spawner():
def __init__(self):
self.pod_name="Test"
def bot_creater(self,bot_nr):
for i in range(5):
print(bot_nr + " doing its work")
time.sleep(2)
def add_thread(self):
threading.Thread(name="Something", target=self.bot_creater, args=("1")).start()
This is perfectly okay, a thread
spawns and we are golden however if I change the args input to args=("bot_1")
instead of just args=("1")
, "it throws me a bot_creater() takes 2 positional arguments but 6 were given"
error.
Any and all help would be much appreciated!
Upvotes: 0
Views: 178
Reputation: 5613
You can also use a list
instead if you have one argument, as the following:
threading.Thread(name="Something", target=self.bot_creater, args=["bot_1"]).start()
Upvotes: 1
Reputation: 1539
TemporalWolf is right, it takes a tuple. If you just put brackets round a string, it is just taken to be mathematical brackets. If you add a comma, python interprets it as a tuple:
threading.Thread(name="Something", target=self.bot_creater, args=("bot_1",)).start()
Upvotes: 1
Reputation: 7952
It's converting the string input into a tuple, like so:
tuple(("bob"))
('b', 'o', 'b')
because
>>> type(("bob"))
<type 'str'>
Instead, you want:
>>> tuple(("bob",))
('bob',)
because
>>> type(("bob",))
<type 'tuple'>
Essentially it's ignoring the extra set of parentheses until you give it (element,)
which then forces it to interpret it as a tuple of length 1.
Upvotes: 1