Q:

Passing formatted strings to functions as arguments

I'm wondering if there is a good way to accomplish the following: I want to run through a for loop and create instances of objects with iterative and sometimes random attributes. In particular, I'm trying to say something like this:
OUT_TASK = "Task_{id1}, FUNCTION, {id2}, task{id3}_types, None"
t = Task(OUT_TASK.format(id1=i, id2='doNothing', id3=i))

Right now the compiler is telling me that I'm only passing 2 arguments to Task() whereas it expect at least 4.
Many Thanks - clues are appreciated.

A:

What your code is doing right now is passing a single string to the Task() constructor. What you want instead is to format each of those arguments individually:
t = Task("Task_{0}".format(i), "FUNCTION", id2, "task{0}_types".format(i), None)

If you don't know ahead of time how many arguments you're going to have, you could build a list and pass it with the *args approach:
params = []
params.append("Task_{0}".format(i))
# ...
params.append("task{0}_types".format(i))
params.append(None)
t = Task(*params)

Although a better approach would be a dictionary (the **kwargs approach), since that way you don't have to care about the order of your constructor params:
params = {}
params['something'] = "Task_{0}".format(i)
params['something_else'] = "task{0}_types".format(i)
# ...
params['last_thing'] = None
t = Task(**params)

