非标准可选参数默认值

发布于 2024-07-27 15:43:21 字数 426 浏览 4 评论 0原文

我有两个函数:

def f(a,b,c=g(b)):
    blabla

def g(n):
    blabla

c 是函数f 中的可选参数。 如果用户未指定其值,则程序应计算 g(b),这将是 c 的值。 但代码无法编译 - 它说名称“b”未定义。 如何解决这个问题?

有人建议:

def g(b):
    blabla

def f(a,b,c=None):
    if c is None:
        c = g(b)
    blabla

但这不行。 也许用户希望 c 为 None,然后 c 将具有另一个值。

I have two functions:

def f(a,b,c=g(b)):
    blabla

def g(n):
    blabla

c is an optional argument in function f. If the user does not specify its value, the program should compute g(b) and that would be the value of c. But the code does not compile - it says name 'b' is not defined. How to fix that?

Someone suggested:

def g(b):
    blabla

def f(a,b,c=None):
    if c is None:
        c = g(b)
    blabla

But this doesn't work. Maybe the user intended c to be None and then c will have another value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最舍不得你 2024-08-03 15:43:21
def f(a,b,c=None):
    if c is None:
        c = g(b)

如果 None 可以是 c 的有效值,那么您可以执行以下操作:

sentinel = object()
def f(a,b,c=sentinel):
    if c is sentinel:
        c = g(b)
def f(a,b,c=None):
    if c is None:
        c = g(b)

If None can be a valid value for c then you do this:

sentinel = object()
def f(a,b,c=sentinel):
    if c is sentinel:
        c = g(b)
那小子欠揍 2024-08-03 15:43:21

你不能那样做。

在函数内部,检查是否指定了 c。 如果不是,请进行计算。

def f(a,b,c=None):
    if c == None:
        c = g(b)
    blabla

You cannot do it that way.

Inside the function, check if c is specified. If not, do the calculation.

def f(a,b,c=None):
    if c == None:
        c = g(b)
    blabla
(り薆情海 2024-08-03 15:43:21

c 的值将在编译时求值 (g(b))。 因此,您需要在 f 之前定义 g 。 当然,您还需要在该阶段定义一个全局 b 变量。

b = 4

def g(a):
    return a+1

def test(a, c=g(b)):
    print(c)

test(b)

打印 5.

value of c will be evaluated (g(b)) at compilation time. You need g defined before f therefore. And of course you need a global b variable to be defined at that stage too.

b = 4

def g(a):
    return a+1

def test(a, c=g(b)):
    print(c)

test(b)

prints 5.

痴意少年 2024-08-03 15:43:21

问题

sentinel = object()
def f(a, b, c=sentinel):
  if c is sentinel:
    c = g(b)

是哨兵是全局/公共的,除非此代码是函数/方法的一部分。 因此,有人可能仍然可以调用 f(23, 42, sentinel)。 但是,如果 f 是全局/公共的,则可以使用闭包将 sentinel 设为本地/私有,以便调用者无法使用它:

def f():
  sentinel = object()
  def tmp(a, b, c=sentinel):
    if c is sentinel:
      c = g(b)
  return tmp
f = f()

如果您担心静态代码分析器可能会对 f 产生错误的想法,那么,您可以对工厂使用相同的参数:

def f(a, b, c=object()): #@UnusedVariable
  sentinel = object()
  def tmp(a, b, c=sentinel):
    if c is sentinel:
      c = g(b)
  return tmp
f = f(23, 42)

The problem with

sentinel = object()
def f(a, b, c=sentinel):
  if c is sentinel:
    c = g(b)

is that sentinel is global/public unless this code is part of a function/method. So someone might still be able to call f(23, 42, sentinel). However, if f is global/public, you can use a closure to make sentinel local/private so that the caller cannot use it:

def f():
  sentinel = object()
  def tmp(a, b, c=sentinel):
    if c is sentinel:
      c = g(b)
  return tmp
f = f()

If you are concerned that static code analyzers could get the wrong idea about f then, you can use the same parameters for the factory:

def f(a, b, c=object()): #@UnusedVariable
  sentinel = object()
  def tmp(a, b, c=sentinel):
    if c is sentinel:
      c = g(b)
  return tmp
f = f(23, 42)
混吃等死 2024-08-03 15:43:21
def f(a,b,*args):
    if len(args) == 1:
        c = args[0]
    elif len(args) == 0:
        c = g(b)
    else:
        raise Exception('Function takes 2 or 3 parameters only.')
    blabla

def g(n):
    blabla

您可能可以更好地构建它,但这就是主要思想。 或者,您可以使用 **kwargs 并使用诸如 f(a,b,c=Something) 之类的函数,您只需修改 f因此。

文档

def f(a,b,*args):
    if len(args) == 1:
        c = args[0]
    elif len(args) == 0:
        c = g(b)
    else:
        raise Exception('Function takes 2 or 3 parameters only.')
    blabla

def g(n):
    blabla

You can probably structure it better, but that's the main idea. Alternatively you can use **kwargs and use the function like f(a,b,c=Something), you just have to modify f accordingly.

Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文