[英]Can I use python return function to only return the value to the specific function
If I have this如果我有这个
def fun1:
a = 2
b = 3
return a
return b #problem is here
def fun2:
c = fun1()
print(fun1())
I want to return a
be a default return and fun1 return a to every other functions but return b
only works for returning to fun2 and nowhere else我想将 a
return a
为默认返回,而 fun1 将 a 返回给所有其他函数,但return b
仅适用于返回 fun2 而无其他地方
What you can do is create an optional parameter for the function which decides what value to return.您可以做的是为 function 创建一个可选参数,该参数决定返回什么值。
def fun1(arg=0):
a = 2
if arg != 0:
a = 3
return a
def fun2():
c = fun1(1)
print(fun1())
fun2()
Here if you call fun1() without any arguments, it will return you 2. But if you give it any integer argument [ example: fun1(2) or fun1(1) ], it will return you 3.在这里,如果您在没有任何 arguments 的情况下调用 fun1(),它将返回 2。但如果您给它任何 integer 参数 [例如:fun1(2) 或 fun1(1)],它将返回 3。
Whenever return a
is executed - it EXITS the function fun1
.每当执行
return a
- 它退出 function fun1
。
So the line return b
will NEVER be executed.所以这行
return b
永远不会被执行。
You can do something like:您可以执行以下操作:
def fun1(return_b=False):
a = 2
b = 3
if return_b:
return b
else:
return a
def fun2:
c = fun1(return_b=True)
Read more about Default Argument Values here . 在此处阅读有关默认参数值的更多信息。
If you want to return both a
and b
, you can return a tuple of them: return a, b
如果你想返回
a
和b
,你可以返回它们的一个元组: return a, b
A function can only return one output at a time.一个 function 一次只能返回一个 output。 Meaning if you have more than one
return
statements in the quesiton, only the first one would be executed and any line written after that statement won't be executed.这意味着如果您在问题中有多个
return
语句,则只会执行第一个语句,并且不会执行该语句之后编写的任何行。
In your example you typed return a
before return b
meaning the function would only return the value of a and the return b
line won't be executed.在您的示例中,您在
return b
之前输入return a
return a ,这意味着 function 只会返回 a 的值,而不会执行return b
行。
From your comment on your own question I can see that you want to print a
first and then want the value of c
to be 3. For that you can do this -从您对自己问题的评论中,我可以看到您想先打印,然后希望
c
a
值为 3。为此,您可以这样做-
def fun1():
a = 2
b = 3
print(a)
return b
def fun2():
c = fun1()
fun1()
You just need two different functions.你只需要两个不同的功能。
Everyone just calls fun1()
, but only fun2()
calls fun1a()
:每个人都只调用
fun1()
,但只有fun2()
调用fun1a()
:
def fun1():
a = 2
b = 3
return a
def fun1a():
a = 2
b = 3
return b
def fun2():
c = fun1a()
return c
print(fun1())
print(fun2())
Here is only an answer to your strange idea: first, get the current frame through inspect.currentframe
, and then get the frame called by the previous layer through the current frame and inspect.getframeinfo
.这里只是回答你的奇思妙想:首先通过
inspect.currentframe
获取当前帧,然后通过当前帧和inspect.getframeinfo
获取上一层调用的帧。 The function name will be recorded in the returned value, and different values can be returned by comparing the function name.返回值中会记录function名称,通过比较function名称可以返回不同的值。
import inspect
def fun1():
a = 2
b = 3
curr_frame = inspect.currentframe()
back_frameinfo = inspect.getframeinfo(curr_frame.f_back)
return b if back_frameinfo.function == 'fun2' else a
def fun2():
return fun1()
print(fun1(), fun2()) # 2 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.