[pythran] Re: multiple default arguments

  • From: Jochen S <cycomanic@xxxxxxxxx>
  • To: pythran@xxxxxxxxxxxxx
  • Date: Sat, 7 Nov 2020 20:34:22 +0100

On Fri, Nov 6, 2020 at 9:24 AM Serge Guelton <
serge.guelton@xxxxxxxxxxxxxxxxxxx> wrote:

On Fri, Nov 06, 2020 at 09:03:34AM +0100, Jochen S wrote:
Hi

if I understand #1243 correctly I cannot export a function with  multiple
default arguments, but only use the second or third. Something along the
lines
of:

#pythran export test(float64, int (int, int))
#pythran export test(float64, (int, int))
#pythran export test(float64, int)
#pythran export test(float64)
def test(x, i=0, t=(1,2)):
    print(x)
    print(i)
    print(t)


At least that doesn't seem to work for me. and just specifying the first
and
last line I would always have to either pass i and t or none of them
correct?

Is there are way of working around this, except for writing a python
wrapper?

Hi Jochen,

Your understanding is correct.

#pythran export test(float64, int, (int, int))
#pythran export test(float64, int)
#pythran export test(float64)

these three are legits, but not that one

#pythran export test(float64, (int, int))

without more info, there's no way for pythran to compute whether this is

i = (int, int) and t = (1,2)

or

i = 0 and t = (int, int)

we would need a new syntax for that. Any proposition?


I think it would be good to have a way of allowing only a subset of keyword
arguments, it seems at the moment pythran is not giving the full power of
python (and I did find it a bit counterintuitive that it does not work).

One way of maybe would be to introduce a empty keyword:

#pythran export test(float64, int or empty, (int, int) or empty)

This has the advantage of making specifying optional arguments more
explicit and would be more in line with the "or" syntax.

One could then also enable the long form:
#pythran export test(float64, int, (int, int))
#pythran export test(float64, empty, (int, int))
#pythran export test(float64, int, empty)

Not sure if empty is the best  keyword here, but can't think of a better one

Other related posts: