Thursday, February 24, 2011

Python lists as function parameters

I often stumble about something like this:
A function needs 2 parameters like
myfunction(a,b)
and i have them, but inside a list 'myList'=[a,b].
Sure I could write
myfunction(myList[0],myList[1]),
but where's the famous Python beauty in this, right?
Until I realised that this is what the *args thingie is for that I never used before.
Damn, all those typing hours lost when I typed the explicit unpacking of lists!!!
Now I can just do
myfunction(*myList)
and all is beautiful again! ;)
Thank you, Python

No comments:

Post a Comment