Python - Why can't default parameters be placed before required parameters when defining a function?

Share a big cow's artificial intelligence tutorial. Zero-based! Easy to understand! Funny and humorous! I hope you will also join the team of artificial intelligence! Please click on the artificial intelligence tutorial

Let's try defining such a function directly and see what the Python interpreter tells us.

As follows, the Python interpreter will report an error, telling us that there is a syntax error and that non-default parameters cannot be placed after default parameters.

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def foo(p1, p2=6, p3):
	return 0
SyntaxError: non-default argument follows default argument
>>> 

Why doesn't the Python interpreter let us put non-default parameters after default parameters?

Putting aside the above error, let's try to call the above function: foo(1, 2). At this time, should we call foo(1, 6, 2)? Or should we call foo(1, 2)? Or call something else? ...

It can be seen that putting default parameters in front of mandatory parameters will cause ambiguity when calling, so the Python interpreter will report an error telling us that we cannot do this.

Guess you like

Origin blog.csdn.net/chimomo/article/details/41649805