简体   繁体   English

如何将可选的python参数传递给带有可选参数的子函数

[英]How to pass optional python arguments to sub-functions with optional arguments

Given a simple Python function with an optional argument, like: 给定一个带有可选参数的简单Python函数,例如:

def wait(seconds=3):
    time.sleep(seconds)

How do I create a function that calls this and passes on an optional argument? 如何创建一个调用此函数并传递可选参数的函数? For example, this does NOT work: 例如,这不起作用:

def do_and_wait(message, seconds=None):
    print(message)
    wait(seconds)

Note : I want to be able to call wait from other functions with the optional seconds argument without having to know and copy the current default seconds value in the underlying wait function to every other function which calls it. 注意 :我希望能够使用可选的seconds参数从其他函数调用wait ,而不必知道并将底层的wait函数中的当前默认seconds值复制到其他调用它的函数中。

As above, if I call it with the optional argument, like do_and_wait(2) then it works, but trying to rely on wait 's default, eg calling it like do_and_wait() causes a TypeError because inside wait seconds == None. 如上所述,如果我使用诸如do_and_wait(2)类的可选参数来调用它,则它可以工作,但是尝试依赖wait的默认值,例如,像do_and_wait()这样调用它会导致TypeError,因为在wait seconds == None内。

Is there a simple and clean way to make this work? 有没有一种简单而干净的方法可以完成这项工作? I know I can abuse kwargs like this: 我知道我可以像这样滥用kwargs:

def do_and_wait(message, **kwargs):
    print(message)
    wait(**kwargs)

But that seems unclear to the reader and user of this function since there is no useful name on the argument. 但这对于该函数的读者和用户来说似乎不清楚,因为参数上没有有用的名称。

Note: This is a stupidly simplified example. 注意:这是一个愚蠢的简化示例。

I don't think you've quite explained your problem completely because I don't expect an answer should be this simple, but I would just use the same default value (and data type) in do_and_wait() as wait() uses, like so: 我不认为您已经完全解释了问题,因为我不希望答案这么简单,但是我只会在do_and_wait()中使用与wait()相同的默认值(和数据类型),像这样:

def do_and_wait(message, seconds=3):
    print(message)
    wait(seconds)

I understand you've simplified your question, but I think you mean how one can call a function with optional arguments that could be None. 我了解您已简化了您的问题,但我想您的意思是人们如何调用带有可选参数(可能为None)的函数。 Does the following work for you? 以下内容对您有用吗?

import time

def func1(mess, sec):
  if sec != None:
    time.sleep(sec)
  print(mess)


func1('success', sec=None)

After thinking a bit more, I came up with something like this; 经过多想之后,我想到了类似的东西。 Han's answer suggested this and reminded me that I think PEP even suggests it somewhere. 韩的回答暗示了这一点,并提醒我,我认为PEP甚至在某处提出了建议。 This especially avoids having to know and copy the default value into any function that calls wait and wants to support a variable value for seconds . 这尤其避免了必须知道默认值并将其复制到任何调用wait并希望在seconds支持变量值的函数中。

def wait(seconds=None):
    time.sleep(seconds if seconds is not None else 3)

def do_and_wait(message, seconds=None):
    print(message)
    wait(seconds)

def something_else(callable, seconds=None):
    callable()
    wait(seconds)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM