Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning a splat
#1
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?
Reply
#2
The only thing I can think of is to rewrite bar so it can do the unpacking itself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply
#4
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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.
Reply
#6
Quack.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020