Skip to content
Advertisement

Python function that takes one compulsory argument from two choices

I have a function:

JavaScript

However, my intention is for the function to take only one argument (either title OR pageid). For example, delete(title="MyPage") or delete(pageid=53342) are valid, while delete(title="MyPage", pageid=53342), is not. Zero arguments can not be passed. How would I go about doing this?

Advertisement

Answer

There is no Python syntax that’ll let you define exclusive-or arguments, no. You’d have to explicitly raise an exception is both or none are specified:

JavaScript

The full expression can become a little tedious, especially if title or pageid could be set to 0 or another falsey value that is valid, so you could capture the above in a utility function:

JavaScript

Note that this is the inverse test of what you needed for the if test, so don’t forget to add not:

JavaScript

which is a lot easier to use when having to test for not None:

JavaScript

Note that I used type hints to add information to the function for type checkers and IDEs to see what kind of arguments the function takes or what the return value type is.

With type hints, you can also mark up your original function and tell those tools that your function only takes one or the other argument, by using @overload:

JavaScript

In an IDE like PyCharm or Visual Studio Code (with the Pylance extension installed) would show you real-time information about the function as you type:

Screenshot of a snippet of code in Visual Studio Code reading "delete()', with a tooltip showing "(title: str) -> None" and the documentation string for the delete function, together with a counter indicating this is one of two possible completions for this function

Another screenshot of the same snippet of code, this time with the tooltip changed to show "(pageid: int) -> None", with the counter indicating that this is the second of two possible completions

This kind of tool integration would make it easier to use your function without triggering an exception.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement