python not accept keyword arguments

You can use arbitrary argument lists to do this. See http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists

For example:

def somefunc(*args):
    print args[0], args[1]

Calling without keywords:

somefunc(10,20)

Gives:

10 20

Calling with keywords:

somefunc(a=10,b=20)

Gives an error:

TypeError: someFunc() got an unexpected keyword argument 'a'

It's unclear why you would want to do this though.


ad 1) Correct names are verified automatically by Python if they are called like somefunct(name=value, ...). I need not to remember the exact standard order of parameters and to verify it too "neurotic" by looking into the documentation every month at every usage, if I remember a function with nice descriptive names of parameters and it will be tested that they are accepted by Python. On the contrary, the correct order of used parameters can be verified only by documentation. Calling by named parameters is preferred over very long list of positional parameters. Therefore the reported behaviour is well-founded. (Short single letter parameters "a, b" don't help against mistakes of course.)

ad 2) Some well known builtin fast functions written in C with small fixed number of required parameters do not support calling with named parameters. (e.g. hasattr)

This is because they use only simple header ...(... PyObject *args) and therefore all named parameters are rejected automatically. (Python can never introspect into names of arguments in C source. :-)

Many other C functions have a header ...(... PyObject *args, PyObject *kwds) and they support exact list of names explicitely by implementing much more complicated validation PyArg_ParseTupleAndKeywords and by writing the names to docs strings.


Edit: Positional-only parameters are possible in Python 3.8 by a new function parameter syntax / to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments.

def somefunc(a, b, /):
    print(a, b)

Looking at the function call somefunc(b=10,a=20) and not the function definition, this can seem to be either of a call to a function which accepts just normal arguments or a function which accepts keyword arguments. How does the interpreter differentiate between the two?

Not sure if this is quite what you're asking, but if I understand you right, the answer is "it doesn't". You can call the function with keywords no matter how the arguments are defined in the function. If you pass any arguments using keyword syntax (e.g., f(a=...)), Python binds the corresponding values to the arguments with the same names in the function definition. It doesn't matter whether a default value was defined. The two kinds of function definition don't create different "kinds" of arguments, it's just that some of the arguments might have default values and others might not. So whether you do def f(a) or def f(a=2), you can still do f(a=...). The only restriction is that you have to pass/define positional arguments first (e.g., you can't do def f(a=2, b) or f(a=2, 3)). See also this answer.

As for the second part of your question, I'm not aware of any way to stop a Python function from accepting keyword-passed values for named arguments. If you define only *args then it won't accept keyword arguments, but the positional arguments can't have separate names.