可选参数,需要一定的组合 [英] Optional Parameters, certain combination of them required

查看:71
本文介绍了可选参数,需要一定的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个一般性的问题以及一个特定的用例.

I have a general question as well as a specific use case.

可选参数很容易:def func(a, b, c=None): ...,然后在体内可能使用c的任何地方,只需先写if c:或类似的内容.但是,什么时候需要参数的特定组合呢?一般情况是考虑存在或不存在确切参数的任意情况.对于功能def func(a, b, c=None, d=None, e=None, f=None): ...,这将包括一些愚蠢的事情,例如:提供c和d但不提供e和f,或仅提供e,或提供c,d,e和f中的至少3个.但是我的用例不需要这种通用性.

Optional parameters are easy enough: def func(a, b, c=None): ... and then anywhere c might be used in the body just write if c: first, or something along those lines. But what about when a certain combination of parameters is required? The general case is to consider any arbitrary situation of which exact parameters exist or not. For a function def func(a, b, c=None, d=None, e=None, f=None): ... this would include silly things like: provide c and d but not e and f, or provide e only, or provide at least 3 of c, d, e, and f. But my use case doesn't require such generality.

对于def func(a, b, c=None, d=None): ...,我希望提供c和d之一.

For def func(a, b, c=None, d=None): ..., I want EXACTLY ONE OF c and d to be provided.

我想到的解决方案包括:
-在正文中,手动检查c和d的多少不为None,如果不完全为1,则返回错误,指出需要指定恰好为1
例如.

Solutions I've thought of include:
- in the body, manually check how many of c and d are not None, and if it's not exactly 1, return an error saying exactly 1 needs to be specified
ex.

def func(a, b, c=None, d=None):
    how_many_provided = len([arg for arg in [c, d] if arg]) # count the non-None optional args
    if not how_many_provided == 1:
        return "Hey, provide exactly 1 of 'c' and 'd'"
    if c:
        # stuff to do if c is provided
    elif d:
        # stuff to do if d is provided

-将函数更改为def func(a, b, e, f): ...,其中e表示c或d,而f表示其中e表示哪个.
前任.

- change the function to be def func(a, b, e, f): ... where e represents either c or d and f indicates which one of those e represents.
ex.

def func(a, b, e, f):
    if f == 'c':
        # stuff to do if c is provided, with e as c
    if f == 'd':
        # stuff to do if d is provided, with e as d

这些方法可行,但是标准/可接受/pythonic的操作方式是什么?

These would work, but what is the standard/accepted/pythonic way of doing this?

推荐答案

我想说的是,在您的简单情况下,对用户而言,最简单的方法是重构为单独的功能.每个功能都会执行上述不同的工作,然后再执行一个通用的功能.最后的情况

I would say the easiest way for your user in your simple case is to refactor to separate functions. Each function does the different work as described and then a common one e.g. for your last case

def funcC(a, b, c):
        # stuff to do if c is provided, with e as c
    common_func(a,b,c, None)

def funcD(a, b, d):
    # stuff to do if d is provided, with e as d
    common_func(a,b,None, d)

然后,用户知道什么参数很重要,并且只能使用有效的可能组合,用户不必猜测或有机会错误地调用它们.您提供该函数可以提供调用者不提供的参数所需的任何内容.

The user then knows what parameters matter and only the valid possible combinations can be used, the user does not have to guess or have a chance to call them incorrectly. You as providing the function can provide whatever is needed for the parameter the caller does not supply.

通过谷歌搜索标志参数"(例如马丁·福勒

There are longer explanations of these found by googling for "flag parameters" e.g. Martin Fowler Stack Overflow these tend to mention Boolean arguments but this in effect the same thing a different code path depending on a parameter which has no other effect.

要寻找的另一句话是控制耦合"

Another phrase to look for is "control coupling"

这篇关于可选参数,需要一定的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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