如果它们是 None,则调用没有可选参数的函数 [英] Call function without optional arguments if they are None

查看:34
本文介绍了如果它们是 None,则调用没有可选参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个接受可选参数的函数.

def alpha(p1="foo", p2="bar"):打印('{0},{1}'.format(p1, p2))

让我重复一下当我们以不同方式使用该函数时会发生什么:

<预><代码>>>>α()富,酒吧>>>阿尔法(FOO")FOO,酒吧>>>阿尔法(p2 =酒吧")富,酒吧>>>阿尔法(p1=FOO",p2=无)FOO,无

现在考虑我想像 alpha("FOO", myp2) 一样调用它的情况,myp2 要么包含要传递的值,要么是 <代码>无.但即使该函数处理 p2=None,我也希望它使用其默认值 "bar" 代替.
也许措辞令人困惑,所以让我改写一下:

<块引用>

如果 myp2 为 None,则调用 alpha("FOO").否则,调用 alpha("FOO", myp2).

区别是相关的,因为 alpha("FOO", None)alpha("FOO") 的结果不同.

我如何简洁地(但可读地)做出这种区分?

一种可能性通常是检查中的Nonealpha,这是值得鼓励的,因为这会使代码更安全.但是假设 alpha 用于其他地方,它实际上应该像它一样处理 None.

我想在调用方处理这个问题.

一种可能是区分大小写:

如果 myp2 是 None:阿尔法(FOO")别的:阿尔法(FOO",myp2)

但是当有多个这样的参数时,这很快就会变成很多代码.(指数,2^n)

另一种可能是简单地执行alpha("FOO", myp2 or "bar"),但是这要求我们知道默认值.通常,我可能会采用这种方法,但稍后我可能会更改 alpha 的默认值,然后需要手动更新此调用,以便仍然使用(新)默认值调用它价值.

我使用的是 python 3.4,但最好是你的答案能提供一种适用于任何 python 版本的好方法.

<小时>

这个问题在技术上已经在这里完成,但我再次改写了一些要求,因为第一个答案确实掩盖了这一点:
我希望 alpha 的行为及其默认值 "foo", "bar" 一般保留,因此它(可能)不是更改 alpha 的选项 本身.
再换句话说,假设 alpha 在其他地方被用作 alpha("FOO", None) 其中输出 FOO,None是预期的行为.

解决方案

将参数作为来自字典的 kwargs 传递,您可以从中过滤掉 None 值:

kwargs = dict(p1='FOO', p2=None)alpha(**{k: v for k, v in kwargs.items() 如果 v 不是 None})

There's a function which takes optional arguments.

def alpha(p1="foo", p2="bar"):
     print('{0},{1}'.format(p1, p2))

Let me iterate over what happens when we use that function in different ways:

>>> alpha()
foo,bar
>>> alpha("FOO")
FOO,bar
>>> alpha(p2="BAR")
foo,BAR
>>> alpha(p1="FOO", p2=None)
FOO,None

Now consider the case where I want to call it like alpha("FOO", myp2) and myp2 will either contain a value to be passed, or be None. But even though the function handles p2=None, I want it to use its default value "bar" instead.
Maybe that's worded confusingly, so let me reword that:

If myp2 is None, call alpha("FOO"). Else, call alpha("FOO", myp2).

The distinction is relevant because alpha("FOO", None) has a different result than alpha("FOO").

How can I concisely (but readably) make this distinction?

One possibility would usually be to check for None within alpha, which would be encouraged because that would make the code safer. But assume that alpha is used in other places where it is actually supposed to handle None as it does.

I'd like to handle that on the caller-side.

One possibility is to do a case distinction:

if myp2 is None:
    alpha("FOO")
else:
    alpha("FOO", myp2)

But that can quickly become much code when there are multiple such arguments. (exponentially, 2^n)

Another possibility is to simply do alpha("FOO", myp2 or "bar"), but that requires us to know the default value. Usually, I'd probably go with this approach, but I might later change the default values for alpha and this call would then need to be updated manually in order to still call it with the (new) default value.

I am using python 3.4 but it would be best if your answers can provide a good way that works in any python version.


The question is technically finished here, but I reword some requirement again, since the first answer did gloss over that:
I want the behaviour of alpha with its default values "foo", "bar" preserved in general, so it is (probably) not an option to change alpha itself.
In yet again other words, assume that alpha is being used somewhere else as alpha("FOO", None) where the output FOO,None is expected behaviour.

解决方案

Pass the arguments as kwargs from a dictionary, from which you filter out the None values:

kwargs = dict(p1='FOO', p2=None)

alpha(**{k: v for k, v in kwargs.items() if v is not None})

这篇关于如果它们是 None,则调用没有可选参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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