-2

Is it possible to run a function like this:

def call(a,*alphabets,*numbers):
    print(a)
    print(alphabets)
    print(numbers)

I'm getting the following error:

  File "<ipython-input-331-ddaef8a7e66f>", line 1
    def call(a,*alphabets,*numbers):
                          ^
SyntaxError: invalid syntax

Can somebody tell me if there's an alternative way to do this?

IndigoChild
  • 842
  • 3
  • 11
  • 29
  • 3
    No, the `*` is greedy, it will consume all remaining (positional) arguments. In any case, how could Python know how to split them between the two different variables? – Graipher Feb 23 '18 at 11:42
  • True, I'm not sure how. I have an assignment which needs me to do something like this. Do you know of any alternative? – IndigoChild Feb 23 '18 at 11:45
  • hi @Omar, I need atleast 2 variable number of arguments to be passed in a function. – IndigoChild Feb 23 '18 at 11:47
  • 1
    Why would you need smth like that? "*2 variable number of argument*" makes no sense because the 2nd variable arument number would be automatically invalidated by the 1st. If you can't make it using `*args, **kwargs`, then either the design is poor, or *Python* is not the appropriate language for this task. – CristiFati Feb 23 '18 at 11:51

1 Answers1

4

Quite simply: require the caller to pass two lists (or tuples or whatever):

def call(a,alphabets=None,numbers=None):
    if alphabets is None:
        alphabets = []
    if numbers is None:
        numbers = []
    print(a)
    print(alphabets)
    print(numbers)


call("?")
call("?", ["a", "b", "c"])
call("?", ["a", "b", "c"], (1, 2, 3))
call("?"), None, (1, 2, 3))
# etc
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118