简体   繁体   English

我可以使用 python return function 只返回值到具体的 function

[英]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如果你想返回ab ,你可以返回它们的一个元组: 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.

相关问题 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? Python:如何使用 lambda function 返回特定值? - Python : How to use lambda function to return a specific value? python装饰器修改的函数的返回值是否只能是Nonetype - Whether the return value of a function modified by python decorator can only be Nonetype 无法使创建的 python function 返回一个值,然后我可以在另一个计算中使用该值 - Can't make created python function return a value which I can then use in another calculation 如何在 Python 中使用 zip 函数并返回包含最大值的系列的索引? - How can I use zip function in Python and return index of the series containing max value? 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function 为什么我不能在 python 的 lambda 函数中使用“return”? - Why can't I use “return” in lambda function in python? 我怎样才能得到一个递归的python函数来返回一个值? - How can I get a recursive python function to return a value? 如何从python中的函数打印和返回值? - How can i print and return a value from a function in python? 当 python 没有返回时,如何访问另一个 function 中的值? - How can I access value in another function when there is no return at python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM