-2

I have couple of functions on a python code and I want to use value hints. This function does not give me any errors:

def function1(self, a: int, b: int = 0):

But when I want to add another parameter to the function it gives me an error:

def function1(self, a: int, b: int = 0, c: int):

SyntaxError: non-default argument follows default argument

Do you know if there is any way to solve this, so that I can have a non-default argument following a default argument?

2
  • 1
    How do you expect to call that, provide a value for c but let b take the default? If you want c to be keyword-only see e.g. stackoverflow.com/a/37829651/3001761, but this isn't related to the type hints - you'd have the same problem without them. Commented Oct 4, 2022 at 8:37
  • What do you think is a „Value hint“? In Python, default values and type homes are mechanisms. Commented Oct 5, 2022 at 5:16

2 Answers 2

0

In python, arguments with default values must follow arguments with required values.

Try rewriting as def function1(self, a: int, c: int, b: int = 0,):

You can still call it by doing function1(self, a=a_val, b=b_val, c=c_val)

Sign up to request clarification or add additional context in comments.

Comments

0

The error says: you can not make argument with no default value in parameter. that is a syntax rule from python. to make it works just add default values like this:

def function1(self, a: int, b: int = 0, c: int = 0):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.