Bottom Page

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?
Quote
#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

Quote
#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
Quote
#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.
Clunk_Head likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#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.
Quote
#6
Quack.
Clunk_Head likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  API call returning list value of 'None' jimbone30 5 268 Jun-14-2019, 07:42 PM
Last Post: jimbone30
  Mysql returning number of rows not data AkaAndrew123 4 365 Jun-10-2019, 02:31 PM
Last Post: AkaAndrew123
  A question about subprocess taking input from command line and returning output! Aurimas 8 307 May-15-2019, 04:02 PM
Last Post: Aurimas
  problem returning values Naito 7 344 Jan-19-2019, 08:21 PM
Last Post: Naito
  colorama/termcolor not returning coloured lines kapilan15 3 326 Jan-14-2019, 12:57 PM
Last Post: buran
  Function not returning correct value ActualNoob 3 327 Jan-11-2019, 12:35 AM
Last Post: stullis
  Returning true or false in a for loop bbop1232012 3 352 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  LDAP code to query for host not returning data burvil 2 474 Oct-17-2018, 10:03 PM
Last Post: burvil
  Prompting user for number, reading number, squaring it and returning result JHPythonLearner 5 529 Sep-13-2018, 04:05 PM
Last Post: gruntfutuk
  Function not returning expected value Euqinu 4 531 Sep-10-2018, 12:48 PM
Last Post: Euqinu

Forum Jump:


Users browsing this thread: 1 Guest(s)