If I write my function f as this:
def f(a, b, c, d = 4, e = 5, f = 6):
print(a, b, c, d, e, f)
I can call it normally with positional arguments:
f(1, 2, 3)
And I can also call it using the positional arguments as keyword arguments:
f(c=3, a=1, b=2)
However, if I do that with the print function, I get an error:
>>> print(value=42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'value' is an invalid keyword argument for this function
I tried to use value as keyword because it was what it showed me when I did help(print)
:
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
edit:
Now I understand the *objects and I get that print is looking for a tuple of values. The pow function illustrates what I ask better:
>>> pow(x=2, y=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pow() takes no keyword arguments
Why can't I never seem to refer to the parameters by their names if I use python functions? As I showed above it worked for my f function.