且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅从Python中的函数获取返回值?

更新时间:2023-11-23 18:55:34

打印"Hello world"是一种所谓的副作用-该功能产生的某种结果未反映在返回值中.您要问的是如何调用该函数两次,一次产生副作用,一次捕获函数返回值.

The printing of "Hello world" is what's known as a side effect - something produced by the function which is not reflected in the return value. What you're asking for is how to call the function twice, once to produce the side effect and once to capture the function return value.

实际上,您根本不必两次调用它-一次足以产生两个结果.只需捕获一次调用中的返回值即可:

In fact you don't have to call it twice at all - once is enough to produce both results. Simply capture the return value on the one and only call:

if input_user == "A":
    variable_a = deduct(variable_a)
else: