2

I have two functions, the latter of which is dependent on the results of the first. They are both mathematical functions using floats.

The gist of the two functions are:

def function1(a,aa,b,bb):
    # get result z from a and b
    # get error in result, zz, from raw errors, aa and bb
    print (z,zz)

def function2(z,zz,c,cc):
    # get result x from z and c
    # get error in result, xx, from zz and cc
    print (x,xx)

With a, b, and c being measured variables defined outside of the functions With aa, bb, and c being the errors in the measured variables, also defined outside of the functions.

After defining the functions, I write the following command:

function2(function1(a,aa,b,bb),c,cc)

I am hoping that function1 would produce two results (z, zz) and they would be accepted as the first two parameters of function2.

Instead, I get the error "TypeError: function2() missing 1 required positional argument: 'cc'

So I assume that function1 is only producing one result at the end. How do I fix this?

Full code here: http://tinyurl.com/sof-typeerror

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
Pingk
  • 532
  • 2
  • 7
  • 17

1 Answers1

1

If your function returns more than 1 argument, then these arguments are packed into a tuple. You should unzip the arguments returned with a * to pass them as parameters to the other function. But, as a convention, you can't unzip anything as a first argument, so, I would rather suggest the following.

function2( *(function1(a,aa,b,bb) + (c, cc)) ) # would work

function2(*function1(a,aa,b,bb),c,cc)          # won't work

If you are really interested as to why the latter won't work or why the convention, then visit - http://www.python.org/dev/peps/pep-3132/ or Unpacking arguments: only named arguments may follow *expression

Also, as suggested in the comments, you should return the parameters & not just print them. Like below.

def function1(a,aa,b,bb):
    get result z from a and b
    get error in result, zz, from raw errors, aa and bb
    print (z,zz)
    return z, zz

def function2(z,zz,c,cc):
    get result x from z and c
    get error in result, xx, from zz and cc
    print (x,xx)
    return x, xx
Community
  • 1
  • 1
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
  • Thank you, I have made the changed you suggested, but I now receive the popup "Only named arguments can follow *expression". and I am sent back to find: function2(*function1(a,aa,b,bb),_c, cc Where the underscore is a highlighted red space – Pingk Nov 17 '13 at 14:25
  • Hmm... I am now receiving the error "TypeError: can only concatenate tuple (not "list") to tuple" – Pingk Nov 17 '13 at 14:37
  • Oops, my bad! Just make the `[c, cc]` to `(c, cc)` in the call to `function1` – shad0w_wa1k3r Nov 17 '13 at 14:42
  • 1
    @Pingk: There is a proposal to allow the second `f2(*f1(a, b), c, d)` syntax also. [pep 448: "Additional Unpacking Generalizations" is not rejected yet](http://www.python.org/dev/peps/pep-0448/). It seems it won't be [included in Python 3.4](http://docs.python.org/dev/whatsnew/3.4.html) – jfs Nov 17 '13 at 15:25