Python Forum
Returning a splat - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Returning a splat (/thread-19763.html)



Returning a splat - Clunk_Head - Jul-13-2019

I've built a function that makes a list of parameters for another function.
This is intended to be part of a library and the concerns must be separated; otherwise I'd just run the second function within the first, but I cannot.

What works now:
def foo(list_1, list_2):
    #do stuff to list_1 and list_2
    return [list_1, list_2]

params = foo(colors, names)
bar(*params)
This works perfectly but I'm preparing this for a client that wants to use only functions and the splat is a bit much for them.
What I'd like is to move the splat into the return statement or anywhere in foo:
def foo(list_1, list_2):
    #do stuff to list_1 and list_2
    return *[list_1, list_2]

params = foo(colors, names)
bar(params)
but this is invalid syntax.

It looks like even in the latest version 3.7.4 unpacking with the splat is still only possible in parameters, list creation, and dictionary creation.

Any thought on how I can return a complete parameters list that does not require the use of a splat?


RE: Returning a splat - ichabod801 - Jul-13-2019

The only thing I can think of is to rewrite bar so it can do the unpacking itself.


RE: Returning a splat - Clunk_Head - Jul-13-2019

(Jul-13-2019, 12:27 PM)ichabod801 Wrote: The only thing I can think of is to rewrite bar so it can do the unpacking itself.

I was afraid that might be the answer. I failed to mention that bar is part of matplotlib, so that makes it a lot tougher.

To expand, foo makes the parameters for plt.legend()
bar is called as:
legend_params = foo(names, colors)
plt.legend(*legend_params)
In my quest for succinctness I may have over simplified my example. Wall


RE: Returning a splat - ichabod801 - Jul-13-2019

(Jul-13-2019, 12:22 PM)Clunk_Head Wrote: a client that wants to use only functions and the splat is a bit much for them.

In the end, I think this is your real problem.


RE: Returning a splat - Clunk_Head - Jul-13-2019

(Jul-13-2019, 12:42 PM)ichabod801 Wrote:
(Jul-13-2019, 12:22 PM)Clunk_Head Wrote: a client that wants to use only functions and the splat is a bit much for them.

In the end, I think this is your real problem.

My hand as it was dealt.

So, I figured it out.
The answer is that I can't.
However, the plt is mutable. So, I can pass the plt as a parameter and apply the legend within the function. Thus eliminating the need to return anything. So my original question was slightly flawed.

Thank you for being my rubber duck on this one.


RE: Returning a splat - ichabod801 - Jul-13-2019

Quack.