如何在外部函数中定义内部函数的参数名称?

时间:2019-06-11 10:06:19

标签: python python-3.x function arguments

我有一个函数f_1,具有不同的默认输入参数,例如arg_1和arg_2。现在,我想用另一个函数f_2调用f_1并更改默认参数之一,让我们说arg_1。如何告诉f_2我想在f_1中更改arg_1?

def f_1(arg_1 = x, arg_2 = y):
    some computations
    return result

def f_2(value_for_arg_f_1, name_arg_f_1):
    do some stuff
    out_f_1 = f_1(name_arg_f_1 = value_for_arg_f_1)
    do some more stuff
    return result_f_2
end_result = f_2(z, arg_2)

那么在示例代码中查找-我必须为f_2中的name_arg_f_1写什么,以便(在计算end_result时)out_f_1 = f_1(arg_2 = z)?

2 个答案:

答案 0 :(得分:1)

您可以使用lambda函数,例如

public class DoorFall : MonoBehaviour {

    private AudioSource doorFallAudioSource;

    // Use this for initializatoin
    void Start() {

        doorFallAudioSource = GetComponent<AudioSource>();

    }

    // Update is called once per frame
    void Update() {

    }

    void OnCollisionEnter (Collision collision) {
        if (collision.gameObject.tag == "player")
        {

            doorFallAudioSource.play();

            Destroy(collision.gameObject);
        }
    }
}

答案 1 :(得分:1)

您可以使用[Python 3.Docs]: Glossary - argument中的示例:

complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})

它依靠[Python]: PEP 448 - Additional Unpacking Generalizations

>>> def f1(arg1=1, arg2=2):
...     print("    Argument 1: [{:}], Argument 2: [{:}]".format(arg1, arg2))
...
>>>
>>> f1()
    Argument 1: [1], Argument 2: [2]
>>>
>>> def f2(f1_arg_val, f1_arg_name):
...     f1(**{f1_arg_name: f1_arg_val})
...
>>>
>>> f2("value for argument 2", "arg2")
    Argument 1: [1], Argument 2: [value for argument 2]