Python> positional arguments / keyword arguments> mixable> print (2, 3, 5, 7, 11, sep ='\ t', end ='')

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 2503 / 12833)

There are the following two arguments.

https://docs.python.jp/3/glossary.html

Positional argument Actual argument Please refer to.

...

keyword argument Actual argument Please refer to.

...

You can mix positional and keyword arguments.

Mixing two arguments> Failure

http://ideone.com/bfvZTn

def check_inputs(name1, value1, name2, value2):
	print('%s: %s' % (name1, value1))
	print('%s: %s' % (name2, value2))

# mixing of [keyword] and [positional] arguments
check_inputs(value1=3.1415, name1="pi", "napier", 2.718)

result


Compilation error	time: 0 memory: 23288 signal:0
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/py_compile.py", line 117, in compile
    raise py_exc
py_compile.PyCompileError:   File "prog.py", line 6
    check_inputs(value1=3.1415, name1="pi", "napier", 2.718)
SyntaxError: non-keyword arg after keyword arg

Positional argument is not good after keyword argument.

Mixing two arguments> Success

http://ideone.com/Kcwf5Q

def check_inputs(name1, value1, name2, value2):
	print('%s: %s' % (name1, value1))
	print('%s: %s' % (name2, value2))

# mixing of [positional] and [keyword] arguments
check_inputs("pi", 3.1415, value2=2.718, name2="napier")

result


Success	time: 0 memory: 23288 signal:0
pi: 3.1415
napier: 2.718

Digression> Remarks

Why shouldn't it be a keyword argument or a positional argument?

I think it's difficult to tell which index argument is after the positional argument.

If there is a positional argument first, it is determined sequentially from 0 of the index, and if it becomes a keyword argument in the middle, it is possible to process the keyword argument from there.

Whether to use mix

I personally don't want to use it (as of March 29, 2017). I feel that the source is hard to read because it is inconsistent.

If you can use it well, you may use it.

Matters taught

@ shiracamus's Comment taught me how to use the mix of positional arguments / keyword arguments.

Thank you for the information.

Recommended Posts

Python> positional arguments / keyword arguments> mixable> print (2, 3, 5, 7, 11, sep ='\ t', end ='')
Positional arguments, keyword arguments, default arguments
Keyword arguments for Python functions
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
I want to memoize including Python keyword arguments