0

I'm sure the answer is somewhere online, but I'm wording it wrong and nothing comes up.

Anyone know how I could take a function like: book.set_colour_RGB(0x21, 251, 228, 228) which requires 3 arguments for R,G and B, and instead use an array RGB=[251,228,228], so that the function would become:

book.set_colour_RGB(0x21, x(RGB)) where x is a mystery function I'm looking for that knows to use 3 arguments because the length of RGB is 3.

I get in this case I could use book.set_colour_RGB(0x21, RGB[0],RGB[1],RGB[2]) and it really wouldn't be that difficult. But I was just curious if there was a way to make it tidier and easier to use, particularly for functions with many more arguments.

Thanks peeps.

1 Answers1

1

What you're looking for is the "unpacking operator":

rgb = [251, 228, 228]
book.set_colour_RGB(0x21, *rgb)

A similar operator ** exists that works with dicts and produces keyword arguments, instead of how * works with lists/tuples and produces positional arguments.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • Thank you so much, this is exactly what I was looking for. And it's super simple. Isn't python great?; I learned about the exec() function yesterday; now this. What a great week this is. – Alfie Stoppani Jul 21 '20 at 16:32