One way to ensure that a method is called with a particular kwarg would be like:
def mymethod(self, *args, **kwargs): assert "required_field" in kwargs
Raising an AssertionError
doesn't seem like the most appropriate thing to do. Is there an agreed upon builtin exception to handle this with a nice error message?
More info: there are 3rd-party subclassing issues where *args and **kwargs kinda' need to be passed so making "required_field" a positional argument is not really a good option.
As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.
**kwargs allows us to pass a variable number of keyword arguments to a Python function. In the function, we use the double-asterisk ( ** ) before the parameter name to denote this type of argument.
Precede double stars ( ** ) to a dictionary argument to pass it to **kwargs parameter. Always place the **kwargs parameter at the end of the parameter list, or you'll get an error.
Python is pretty flexible in terms of how arguments are passed to a function. The *args and **kwargs make it easier and cleaner to handle arguments. The important parts are “*” and “**”. You can use any word instead of args and kwargs but it is the common practice to use the words args and kwargs.
Note that you don’t need to specify the exception object in the raise statement. In this case, Python knows that the raise statement will raise the current exception that has been caught by the except clause. And you’ll see both the logging message and the exception in the output:
The names `args` and `kwargs` are used by convention, they are not a part of the language specification. Thus, these are equivalent: If any positional argument follow `*args`, they are keyword-only arguments that can only be passed by name.
Note: Exception is the base class for all the exceptions in Python. You can check the exception hierarchy here . Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.
As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.
>>> def foo(bar): pass ... >>> foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() missing 1 required positional argument: 'bar'
I'd just go with TypeError..
+1 for TypeError. This is what Python 3 raises for required keyword-only arguments:
>>> def foo(*, x): ... pass ... >>> foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() needs keyword-only argument x >>> foo(2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() takes exactly 0 positional arguments (1 given) >>> foo(x=2)
(TypeError suggestion was already given (and accepted); I wrote this answer to mention this Python 3 feature).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With