1

given:

def psum(a,b,c):
    return a**b+c

and x = [1,2] and y = 3 how can I do psum(*x,3) --> an equivalent.

I do not want to do x[0], x[1] because a function returns x and calling it twice would be inefficient. Could one do z = function(a). where z = x. and then do z[0], z[1].

But I'm wondering if one can do this otherwise and use positional arguments in such a way.

Also, without using a wrapper.

Edit: One cannot use names because I didn't implement the function and the function writers did not use named arguments :/

Eiyrioü von Kauyf
  • 4,481
  • 7
  • 32
  • 41
  • Also, note that if you have `x` -- `x = func(...)`, then you can index `x` naturally. It won't call the function again. `x = func(); psum(x[0],x[1],3)` only calls `func` once. – mgilson Oct 12 '12 at 18:25

2 Answers2

2

You can do this:

psum(*x + [y])
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • it works! and he winss :). you can do any order of *x + [2], [2] + x... etc it seems. now what about interspersed keyword and positional e.g. func(a=2,b,c,d=7) ;) – Eiyrioü von Kauyf Oct 12 '12 at 18:46
1

In this case, the order of your arguments doesn't matter, so psum(3,*x) will work.

In the slightly-more-general case, if you know the names of the arguments,

psum(*x,c=3)

seems to work for me as well, but it won't work if sum is defined so that it takes varargs ...

I don't think there is a way to do this in the completely general case without modifying the object you pass to psum:

x.append(3)
psum(*x)
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • sorry. fixed. order matters. i'm thinking ideally plot(x,y,z) is my problem and i'm using meshgrid(x,y) to get x,y – Eiyrioü von Kauyf Oct 12 '12 at 18:17
  • I think you can do something like `x_out,y_out = meshgrid(x,y)` so that the result is two separate objects. – ely Oct 12 '12 at 18:18
  • The problem is that named arguments cannot follow a `*` pointer type expression. So either name all of the arguments, or write the function to handle an iterable, or use `*args` and `**kwargs` in the definition. I don't think this is very satisfactory on Python's part, but it is what it is. – ely Oct 12 '12 at 18:19
  • @Eiyrioü von Kauyf, check out meshgrid's docs: [link](http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.htm). The main use case is such that you get two separate objects back, rather than a tuple of them. – ely Oct 12 '12 at 18:22
  • @EMS: wrong. 1. Named arguments may follow `*`expression: `f(*[1,2],c=3)` is legal 2.[A function always returns a single object in Python (it may be a tuple)](http://stackoverflow.com/a/61636/4279). – jfs Oct 12 '12 at 18:27
  • Sorry, it was a typo. I meant *unnamed* arguments cannot follow. I thought it was clear what I meant and it was too late to edit the comment. Notice how I say right after that naming all the arguments could be a solution. – ely Oct 12 '12 at 18:29
  • Also, you can unpack the output automatically, as in the linked example from NumPy. It's syntactic sugar, sure, but probably solves the OP's problem. – ely Oct 12 '12 at 18:30
  • @EMS yeah but I want to do plot_wireframe(*meshgrid(x,y),Z) if you get what I mean.. more of the unpythonic-shortness thing ;). – Eiyrioü von Kauyf Oct 12 '12 at 18:34
  • In that case, I would *highly* suggest storing the output to variables first. If it's just an ad hoc plotting script, then it's just a matter of inconvenience for you to write the extra assignment. If it's in a script meant to be used by someone else, the readability matters a lot more than trying to jam it all into a one liner. – ely Oct 12 '12 at 18:38