可选参数,需要一定的组合

编程入门 行业动态 更新时间:2024-10-22 22:54:10
本文介绍了可选参数,需要一定的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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"

更多推荐

可选参数,需要一定的组合

本文发布于:2023-11-30 02:50:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   可选   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!