Keyword argument or positional argument?

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

I guess ‘start’ and ‘stop’ are positional arguments, but what are the rest arguments? Are they positonal or keyword?

They are both–you have the option to use keywords but if you write np.linspace(0, 1, 100, False) those will be interpreted as num and endpoint respectively.

Note that it is also trivially easy (and much faster) to test this yourself.

edit: I’ll just add that start and stop can be passed by keyword as well. They aren’t positional-only arguments. But they are required arguments because they don’t have a default.

1 Like

The default kind of argument in Python can be passed positionally or by keyword, so the distinction you are trying to draw does not make sense.

Positional-only arguments in a function signature will appear before a special entry / in the signature, and keyword-only arguments will come after either a special entry * or after a named “variable number of positional arguments” like *args. (Well, an argument like *args is also positional-only.)

You can read about kinds of arguments in the documentation for the inspect standard library module (which is used for analyzing function objects to find out what parameters they have, programmatically).

2 Likes
In [41]: np.linspace(stop = 10, start =1 )
Out[41]: 
array([ 1.        ,  1.18367347,  1.36734694,  1.55102041,  1.73469388,
        1.91836735,  2.10204082,  2.28571429,  2.46938776,  2.65306122,
        2.83673469,  3.02040816,  3.20408163,  3.3877551 ,  3.57142857,
        3.75510204,  3.93877551,  4.12244898,  4.30612245,  4.48979592,
        4.67346939,  4.85714286,  5.04081633,  5.2244898 ,  5.40816327,
        5.59183673,  5.7755102 ,  5.95918367,  6.14285714,  6.32653061,
        6.51020408,  6.69387755,  6.87755102,  7.06122449,  7.24489796,
        7.42857143,  7.6122449 ,  7.79591837,  7.97959184,  8.16326531,
        8.34693878,  8.53061224,  8.71428571,  8.89795918,  9.08163265,
        9.26530612,  9.44897959,  9.63265306,  9.81632653, 10.        ])

In [42]: np.linspace(stop = 10, start =1 , 4 )
  Cell In[42], line 1
    np.linspace(stop = 10, start =1 , 4 )
                                        ^
SyntaxError: positional argument follows keyword argument

Yes, just test it!

Indeed! The SyntaxError gives you a succinct explanation of what went wrong there. The docs go into more detail, with examples.

1 Like

Indeed. The error message told you something useful: when you call the function - or any other function - you may not write positional arguments after keyword arguments. That is, the values that you are passing positionally have to come first.

But note that this is a complaint that Python makes about syntax, not a complaint caused by the function’s interface; and note that it is about arguments, not parameters.

With respect: it’s important to understand language fundamentals - like the rules for calling functions, passing arguments and such - before trying to use sophisticated third-party libraries like Numpy.

1 Like