0

There are quite a few questions about positional/*args arguments such as this and this, but I cannot use their answers to solve my issue, thus the following question.

I defined a function where it can take an unknown number of additional arguments like this

def my_func(a, b, display=False, *args):
    obj = <build something from a>
    obj.add(b)
    for arg in args:
        obj.add(arg)
    if display:
        print(obj)

    return obj

But when I ran it with an additional argument

my_func(a, b, display=True, c)

I got this error

SyntaxError: positional argument follows keyword argument

As I have little almost to nothing in programming, I cannot understand what the error means. Could you please help me out how to fix it?

Nemo
  • 1,124
  • 2
  • 16
  • 39
  • 3
    Put *args before display=False. – Sohaib Farooqi Aug 29 '19 at 09:09
  • @Sohaib The issue is in the *calling*, not in the function `def`inition. – deceze Aug 29 '19 at 09:11
  • @Nemo In a nutshell, keyword arguments must follow positional arguments when calling: `my_func(a, b, c, display=True)`. – deceze Aug 29 '19 at 09:15
  • Thanks Sohaib and @deceze for your resuce. As I have no programming knowledge, I just blindly copied other people's codes here and there without understanding the fundamentals behind, thus making such mistakes that seem trivial to knowledgeable people. Maybe that's why I couldn't understand why the answer in https://stackoverflow.com/questions/42163846/positional-argument-follows-keyword-argument can be applied in my case. Moreover, I think that question was too specific, not as general as mine? – Nemo Aug 29 '19 at 09:31
  • @deceze, I forgot to mention that before posting the question, I did try `my_func(a, b, c, display=True)` but got this error `TypeError: my_func() got multiple values for argument 'display'` which caused me at my wit's end! – Nemo Aug 29 '19 at 09:37

0 Answers0