从两个选项中获取一个强制参数的 Python 函数 [英] Python function that takes one compulsory argument from two choices

查看:22
本文介绍了从两个选项中获取一个强制参数的 Python 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能:

def delete(title=None, pageid=None):# 删除给定的页面,通过其标题或 ID...经过

然而,我的目的是让函数只接受一个参数(titlepageid).例如,delete(title="MyPage")delete(pageid=53342) 是有效的,而 delete(title="MyPage", pageid=53342),不是.不能传递零参数.我该怎么做?

解决方案

没有 Python 语法可以让您定义异或参数,没有.您必须明确引发异常是两者都指定还是没有指定:

if (title and pageid) or not (title or pageid):raise ValueError('只能按标题或 pageid 删除')

完整的表达式可能会变得有点乏味,特别是如果 titlepageid 可以设置为 0 或其他有效的 falsey 值,因此您可以在实用程序函数中捕获上述内容:

from typing import Anydef exclusive_or(left: Any, right: Any) ->布尔:""""测试左或右是否为真,但不能同时为"""返回(左或右)而不是(左和右)

请注意,这是if 测试所需内容的反向测试,所以不要忘记添加not:

如果不是exclusive_or(title, pageid):# ...

在必须测试 not None 时更容易使用:

如果不是exclusive_or(title不是None,pageid不是None):# ...

请注意,我使用了

这种工具集成将使您更轻松地使用您的功能而不会触发异常.

I have a function:

def delete(title=None, pageid=None):
    # Deletes the page given, either by its title or ID
    ...
    pass

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?

解决方案

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:

if (title and pageid) or not (title or pageid):
    raise ValueError('Can only delete by title OR pageid')

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:

from typing import Any

def exclusive_or(left: Any, right: Any) -> bool:
    """Test if either left or right is true, but not both"""
    return (left or right) and not (left and right)

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

if not exclusive_or(title, pageid):
    # ...

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

if not exclusive_or(title is not None, pageid is not None):
    # ...

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:

@overload
def delete(title: str) -> None: ...

@overload
def delete(pageid: int) -> None: ...

def delete(title: Optional[str] = None, pageid: Optional[int] = None) -> None:
    """Deletes the page given, either by its title or ID"""
    ...
    pass

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:

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

这篇关于从两个选项中获取一个强制参数的 Python 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆